home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  114.3 KB  |  4,016 lines

  1. /* Evaluator for XEmacs Lisp interpreter.
  2.    Copyright (C) 1985-1987, 1992-1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Mule 2.0.  Not synched with FSF. */
  21.  
  22. /* Debugging hack */
  23. int always_gc;
  24.  
  25.  
  26. #include <config.h>
  27. #include "lisp.h"
  28.  
  29. #ifndef standalone
  30. #include "commands.h"
  31. #endif
  32.  
  33. #include "symeval.h"
  34. #include "backtrace.h"
  35. #include "bytecode.h"
  36. #include "buffer.h"
  37. #include "opaque.h"
  38.  
  39. struct backtrace *backtrace_list;
  40.  
  41. /* This is the list of current catches (and also condition-cases).
  42.    This is a stack: the most recent catch is at the head of the
  43.    list.  Catches are created by declaring a 'struct catchtag'
  44.    locally, filling the .TAG field in with the tag, and doing
  45.    a setjmp() on .JMP.  Fthrow() will store the value passed
  46.    to it in .VAL and longjmp() back to .JMP, back to the function
  47.    that established the catch.  This will always be either
  48.    internal_catch() (catches established internally or through
  49.    `catch') or condition_case_1 (condition-cases established
  50.    internally or through `condition-case').
  51.  
  52.    The catchtag also records the current position in the
  53.    call stack (stored in BACKTRACE_LIST), the current position
  54.    in the specpdl stack (used for variable bindings and
  55.    unwind-protects), the value of LISP_EVAL_DEPTH, and the
  56.    current position in the GCPRO stack.  All of these are
  57.    restored by Fthrow().
  58.    */
  59.    
  60. struct catchtag *catchlist;
  61.  
  62. Lisp_Object Qautoload, Qmacro, Qexit;
  63. #ifndef standalone
  64. Lisp_Object Qinteractive, Qcommandp, Qdefun, Qeval, Qprogn, Qvalues;
  65. #endif
  66. Lisp_Object Vquit_flag, Vinhibit_quit;
  67. Lisp_Object Qand_rest, Qand_optional;
  68. Lisp_Object Qdebug_on_error;
  69. Lisp_Object Qstack_trace_on_error;
  70. Lisp_Object Qdebug_on_signal;
  71. Lisp_Object Qstack_trace_on_signal;
  72. Lisp_Object Qdebugger;
  73. Lisp_Object Qinhibit_quit;
  74.  
  75. Lisp_Object Qsetq;
  76.  
  77. Lisp_Object Qdisplay_warning;
  78. Lisp_Object Vpending_warnings, Vpending_warnings_tail;
  79.  
  80. Lisp_Object Vrun_hooks;
  81.  
  82. /* Non-nil means record all fset's and provide's, to be undone
  83.    if the file being autoloaded is not fully loaded.
  84.    They are recorded by being consed onto the front of Vautoload_queue:
  85.    (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide.  */
  86.  
  87. Lisp_Object Vautoload_queue;
  88.  
  89. /* Current number of specbindings allocated in specpdl.  */
  90. static int specpdl_size;
  91.  
  92. /* Pointer to beginning of specpdl.  */
  93. struct specbinding *specpdl;
  94.  
  95. /* Pointer to first unused element in specpdl.  */
  96. struct specbinding *specpdl_ptr;
  97.  
  98. /* specpdl_ptr - specpdl.  Callers outside this this file should use
  99.  *  specpdl_depth () function-call */
  100. static int specpdl_depth_counter;
  101.  
  102. /* Maximum size allowed for specpdl allocation */
  103. int max_specpdl_size;
  104.  
  105. /* Depth in Lisp evaluations and function calls.  */
  106. int lisp_eval_depth;
  107.  
  108. /* Maximum allowed depth in Lisp evaluations and function calls.  */
  109. int max_lisp_eval_depth;
  110.  
  111. /* Nonzero means enter debugger before next function call */
  112. static int debug_on_next_call;
  113.  
  114. /* List of conditions (non-nil atom means all) which cause a backtrace
  115.    if an error is handled by the command loop's error handler.  */
  116. Lisp_Object Vstack_trace_on_error;
  117.  
  118. /* List of conditions (non-nil atom means all) which enter the debugger
  119.    if an error is handled by the command loop's error handler.  */
  120. Lisp_Object Vdebug_on_error;
  121.  
  122. /* List of conditions (non-nil atom means all) which cause a backtrace
  123.    if any error is signalled.  */
  124. Lisp_Object Vstack_trace_on_signal;
  125.  
  126. /* List of conditions (non-nil atom means all) which enter the debugger
  127.    if any error is signalled.  */
  128. Lisp_Object Vdebug_on_signal;
  129.  
  130. /* Nonzero means enter debugger if a quit signal
  131.    is handled by the command loop's error handler.
  132.  
  133.    From lisp, this is a boolean variable and may have the values 0 and 1.
  134.    But, eval.c temporarily uses the second bit of this variable to indicate
  135.    that a critical_quit is in progress.  The second bit is reset immediately
  136.    after it is processed in signal_call_debugger().  */
  137. int debug_on_quit;
  138.  
  139. /* Nonzero means we are trying to enter the debugger.
  140.    This is to prevent recursive attempts. 
  141.    Cleared by the debugger calling Fbacktrace */
  142. static int entering_debugger;
  143.  
  144. /* Function to call to invoke the debugger */
  145. Lisp_Object Vdebugger;
  146.  
  147. /* Chain of condition handlers currently in effect.
  148.    The elements of this chain are contained in the stack frames
  149.    of Fcondition_case and internal_condition_case.
  150.    When an error is signaled (by calling Fsignal, below),
  151.    this chain is searched for an element that applies.
  152.  
  153.    Each element of this list is one of the following:
  154.  
  155.    A list of a handler function and possibly args to pass to
  156.    the function.  This is a handler established with
  157.    `call-with-condition-handler' (q.v.).
  158.  
  159.    A list whose car is Qunbound and whose cdr is Qt.
  160.    This is a special condition-case handler established
  161.    by C code with condition_case_1().  All errors are
  162.    trapped; the debugger is not invoked even if
  163.    `debug-on-error' was set.
  164.  
  165.    A list whose car is Qunbound and whose cdr is Qerror.
  166.    This is a special condition-case handler established
  167.    by C code with condition_case_1().  It is like Qt
  168.    except that the debugger is invoked normally if it is
  169.    called for.
  170.  
  171.    A list whose car is Qunbound and whose cdr is a list
  172.    of lists (CONDITION-NAME BODY ...) exactly as in
  173.    `condition-case'.  This is a normal `condition-case'
  174.    handler.
  175.  
  176.    Note that in all cases *except* the first, there is a
  177.    corresponding catch, whose TAG is the value of
  178.    Vcondition_handlers just after the handler data just
  179.    described is pushed onto it.  The reason is that
  180.    `condition-case' handlers need to throw back to the
  181.    place where the handler was installed before invoking
  182.    it, while `call-with-condition-handler' handlers are
  183.    invoked in the environment that `signal' was invoked
  184.    in.
  185. */
  186. static Lisp_Object Vcondition_handlers;
  187.  
  188. /* Used for error catching purposes by throw_or_bomb_out */
  189. static int throw_level;
  190.  
  191. /* temporary storage locations for various macros, to avoid evaluating
  192.    arguments more than once.  Not used under GCC. */
  193. MAC_DEFINE (Emchar, mactemp_syntax_ch);
  194. MAC_DEFINE (int, mactemp_syntax_int);
  195. MAC_DEFINE (Emchar, mactemp_trt_ch);
  196. MAC_DEFINE (Emchar, mactemp_case_ch);
  197. MAC_DEFINE (CONST Bufbyte *, mactemp_charptr);
  198. MAC_DEFINE (Lisp_Object, mactemp_xrecord);
  199. #ifdef MULE
  200. MAC_DEFINE (Emchar, mactemp_lstream_emchar);
  201. MAC_DEFINE (int, mactemp_lstream_emcint);
  202. #endif
  203.  
  204.  
  205. /**********************************************************************/
  206. /*                     The subr and bytecode types                    */
  207. /**********************************************************************/
  208.  
  209. static void print_subr (Lisp_Object, Lisp_Object, int);
  210. DEFINE_LRECORD_IMPLEMENTATION ("subr", subr,
  211.                                this_one_is_unmarkable, print_subr, 0, 0, 0,
  212.                    struct Lisp_Subr);
  213.  
  214. static void
  215. print_subr (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
  216. {
  217.   struct Lisp_Subr *subr = XSUBR (obj);
  218.  
  219.   if (print_readably)
  220.     error ("printing unreadable object #<subr %s>",
  221.        subr_name (subr));
  222.  
  223.   write_c_string (((subr->max_args == UNEVALLED)
  224.                    ? "#<special-form "
  225.                    : "#<subr "),
  226.                   printcharfun);
  227.     
  228.   write_c_string (subr_name (subr), printcharfun);
  229.   write_c_string (((subr->prompt) ? " (interactive)>" : ">"),
  230.                   printcharfun);
  231. }
  232.  
  233.  
  234. static Lisp_Object mark_bytecode (Lisp_Object, void (*) (Lisp_Object));
  235. extern void print_bytecode (Lisp_Object, Lisp_Object, int);
  236. static int bytecode_equal (Lisp_Object, Lisp_Object, int);
  237. static unsigned long bytecode_hash (Lisp_Object obj, int depth);
  238. DEFINE_LRECORD_IMPLEMENTATION ("compiled-function", bytecode,
  239.                                mark_bytecode, print_bytecode, 0,
  240.                    bytecode_equal, bytecode_hash,
  241.                    struct Lisp_Bytecode);
  242.  
  243. static Lisp_Object
  244. mark_bytecode (Lisp_Object obj, void (*markobj) (Lisp_Object))
  245. {
  246.   struct Lisp_Bytecode *b = XBYTECODE (obj);
  247.  
  248.   ((markobj) (b->bytecodes));
  249.   ((markobj) (b->arglist));
  250.   ((markobj) (b->doc_and_interactive));
  251.   /* tail-recurse on constants */
  252.   return (b->constants);
  253. }
  254.  
  255. static int
  256. bytecode_equal (Lisp_Object o1, Lisp_Object o2, int depth)
  257. {
  258.   struct Lisp_Bytecode *b1 = XBYTECODE (o1);
  259.   struct Lisp_Bytecode *b2 = XBYTECODE (o2);
  260.   return (b1->flags.documentationp == b2->flags.documentationp
  261.       && b1->flags.interactivep == b2->flags.interactivep
  262.       && b1->flags.domainp == b2->flags.domainp /* I18N3 */
  263.       && internal_equal (b1->bytecodes, b2->bytecodes, depth + 1)
  264.       && internal_equal (b1->constants, b2->constants, depth + 1)
  265.       && internal_equal (b1->arglist, b2->arglist, depth + 1)
  266.       && internal_equal (b1->doc_and_interactive, 
  267.                  b2->doc_and_interactive, depth + 1));
  268. }
  269.  
  270. static unsigned long
  271. bytecode_hash (Lisp_Object obj, int depth)
  272. {
  273.   struct Lisp_Bytecode *b = XBYTECODE (obj);
  274.   return HASH3 ((b->flags.documentationp << 2) +
  275.         (b->flags.interactivep << 1) +
  276.         b->flags.domainp,
  277.         internal_hash (b->bytecodes, depth + 1),
  278.         internal_hash (b->constants, depth + 1));
  279. }
  280.  
  281.  
  282. /**********************************************************************/
  283. /*                       Entering the debugger                        */
  284. /**********************************************************************/
  285.  
  286. /* unwind-protect used by call_debugger() to restore the value of
  287.    enterring_debugger. (We cannot use specbind() because the
  288.    variable is not Lisp-accessible.) */
  289.  
  290. static Lisp_Object
  291. restore_entering_debugger (Lisp_Object arg)
  292. {
  293.   entering_debugger = ((NILP (arg)) ? 0 : 1);
  294.   return arg;
  295. }
  296.  
  297. /* Actually call the debugger.  ARG is a list of args that will be
  298.    passed to the debugger function, as follows;
  299.  
  300. If due to frame exit, args are `exit' and the value being returned;
  301.  this function's value will be returned instead of that.
  302. If due to error, args are `error' and a list of the args to `signal'.
  303. If due to `apply' or `funcall' entry, one arg, `lambda'.
  304. If due to `eval' entry, one arg, t.
  305.  
  306. */
  307.  
  308. static Lisp_Object
  309. call_debugger_259 (Lisp_Object arg)
  310. {
  311.   return apply1 (Vdebugger, arg);
  312. }
  313.  
  314. /* Call the debugger, doing some encapsulation.  We make sure we have
  315.    some room on the eval and specpdl stacks, and bind enterring_debugger
  316.    to 1 during this call.  This is used to trap errors that may occur
  317.    when enterring the debugger (e.g. the value of `debugger' is invalid),
  318.    so that the debugger will not be recursively entered if debug-on-error
  319.    is set. (Otherwise, XEmacs would infinitely recurse, attempting to
  320.    enter the debugger.) enterring_debugger gets reset to 0 as soon
  321.    as a backtrace is displayed, so that further errors can indeed be
  322.    handled normally.
  323.  
  324.    We also establish a catch for 'debugger.  If the debugger function
  325.    throws to this instead of returning a value, it means that the user
  326.    pressed 'c' (pretend like the debugger was never entered).  The
  327.    function then returns Qunbound. (If the user pressed 'r', for
  328.    return a value, then the debugger function returns normally with
  329.    this value.)
  330.  
  331.    The difference between 'c' and 'r' is as follows:
  332.  
  333.    debug-on-call:
  334.      No difference.  The call proceeds as normal.
  335.    debug-on-exit:
  336.      With 'r', the specified value is returned as the function's
  337.      return value.  With 'c', the value that would normally be
  338.      returned is returned.
  339.    signal:
  340.      With 'r', the specified value is returned as the return
  341.      value of `signal'. (This is the only time that `signal'
  342.      can return, instead of making a non-local exit.) With `c',
  343.      `signal' will continue looking for handlers as if the
  344.      debugger was never entered, and will probably end up
  345.      throwing to a handler or to top-level.
  346. */
  347.  
  348. static Lisp_Object
  349. call_debugger (Lisp_Object arg)
  350. {
  351.   int threw;
  352.   Lisp_Object val;
  353.   int speccount;
  354.  
  355.   if (lisp_eval_depth + 20 > max_lisp_eval_depth)
  356.     max_lisp_eval_depth = lisp_eval_depth + 20;
  357.   if (specpdl_size + 40 > max_specpdl_size)
  358.     max_specpdl_size = specpdl_size + 40;
  359.   debug_on_next_call = 0;
  360.  
  361.   speccount = specpdl_depth_counter;
  362.   record_unwind_protect (restore_entering_debugger, 
  363.                          (entering_debugger ? Qt : Qnil));
  364.   entering_debugger = 1;
  365.   val = internal_catch (Qdebugger, call_debugger_259, arg, &threw);
  366.  
  367.   return (unbind_to (speccount, ((threw) 
  368.                                  ? Qunbound /* Not returning a value */
  369.                                  : val)));
  370. }
  371.  
  372. /* Called when debug-on-exit behavior is called for.  Enter the debugger
  373.    with the appropriate args for this.  VAL is the exit value that is
  374.    about to be returned. */
  375.  
  376. static Lisp_Object
  377. do_debug_on_exit (Lisp_Object val)
  378. {
  379.   /* This is falsified by call_debugger */
  380.   int old_debug_on_next_call = debug_on_next_call;
  381.   Lisp_Object v = call_debugger (list2 (Qexit, val));
  382.   debug_on_next_call = old_debug_on_next_call;
  383.   return ((!EQ (v, Qunbound)) ? v : val);
  384. }
  385.  
  386. /* Called when debug-on-call behavior is called for.  Enter the debugger
  387.    with the appropriate args for this.  VAL is either t for a call
  388.    through `eval' or 'lambda for a call through `funcall'.
  389.  
  390.    #### The differentiation here between EVAL and FUNCALL is bogus.
  391.    FUNCALL can be defined as
  392.  
  393.    (defmacro func (fun &rest args)
  394.      (cons (eval fun) args))
  395.  
  396.    and should be treated as such.
  397.  */
  398.  
  399. static void
  400. do_debug_on_call (Lisp_Object code)
  401. {
  402.   debug_on_next_call = 0;
  403.   backtrace_list->debug_on_exit = 1;
  404.   call_debugger (list1 (code));
  405. }
  406.  
  407. /* LIST is the value of one of the variables `debug-on-error',
  408.    `debug-on-signal', `stack-trace-on-error', or `stack-trace-on-signal',
  409.    and CONDITIONS is the list of error conditions associated with
  410.    the error being signalled.  This returns non-nil if LIST
  411.    matches CONDITIONS. (A nil value for LIST does not match
  412.    CONDITIONS.  A non-list value for LIST does match CONDITIONS.
  413.    A list matches CONDITIONS when one of the symbols in LIST is the
  414.    same as one of the symbols in CONDITIONS.) */
  415.  
  416. static int
  417. wants_debugger (Lisp_Object list, Lisp_Object conditions)
  418. {
  419.   if (NILP (list))
  420.     return 0;
  421.   if (! CONSP (list))
  422.     return 1;
  423.  
  424.   while (CONSP (conditions))
  425.     {
  426.       Lisp_Object this, tail;
  427.       this = XCAR (conditions);
  428.       for (tail = list; CONSP (tail); tail = XCDR (tail))
  429.     if (EQ (XCAR (tail), this))
  430.       return 1;
  431.       conditions = XCDR (conditions);
  432.     }
  433.   return 0;
  434. }
  435.  
  436. /* Actually generate a backtrace on STREAM. */
  437.  
  438. static Lisp_Object
  439. backtrace_259 (Lisp_Object stream)
  440. {
  441.   return (Fbacktrace (stream, Qt));
  442. }
  443.  
  444. /* An error was signalled.  Maybe call the debugger, if the `debug-on-error'
  445.    etc. variables call for this.  CONDITIONS is the list of conditions
  446.    associated with the error being signalled.  SIG is the actual error
  447.    being signalled, and DATA is the associated data (these are exactly
  448.    the same as the arguments to `signal').  ACTIVE_HANDLERS is the
  449.    list of error handlers that are to be put in place while the debugger
  450.    is called.  This is generally the remaining handlers that are
  451.    outside of the innermost handler trapping this error.  This way,
  452.    if the same error occurs inside of the debugger, you usually don't get
  453.    the debugger entered recursively.
  454.  
  455.    This function returns Qunbound if it didn't call the debugger or if
  456.    the user asked (through 'c') that XEmacs should pretend like the
  457.    debugger was never entered.  Otherwise, it returns the value
  458.    that the user specified with `r'. (Note that much of the time,
  459.    the user will abort with C-], and we will never have a chance to
  460.    return anything at all.)
  461.  
  462.    SIGNAL_VARS_ONLY means we should only look at debug-on-signal
  463.    and stack-trace-on-signal to control whether we do anything.
  464.    This is so that debug-on-error doesn't make handled errors
  465.    cause the debugger to get invoked.
  466.  
  467.    STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that
  468.    those functions aren't done more than once in a single `signal'
  469.    session. */
  470.  
  471. static Lisp_Object
  472. signal_call_debugger (Lisp_Object conditions,
  473.                       Lisp_Object sig, Lisp_Object data,
  474.                       Lisp_Object active_handlers,
  475.               int signal_vars_only,
  476.               int *stack_trace_displayed,
  477.               int *debugger_entered)
  478. {
  479.   /* This function can GC */
  480.   Lisp_Object val = Qunbound;
  481.   Lisp_Object all_handlers = Vcondition_handlers;
  482.   int speccount = specpdl_depth_counter;
  483.   struct gcpro gcpro1;
  484.   GCPRO1 (all_handlers);
  485.  
  486.   Vcondition_handlers = active_handlers;
  487.  
  488.   if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only 
  489.       && wants_debugger (Vstack_trace_on_error, conditions))
  490.     {
  491.       specbind (Qdebug_on_error, Qnil);
  492.       specbind (Qstack_trace_on_error, Qnil);
  493.       specbind (Qdebug_on_signal, Qnil);
  494.       specbind (Qstack_trace_on_signal, Qnil);
  495.       
  496.       internal_with_output_to_temp_buffer ("*Backtrace*",
  497.                        backtrace_259,
  498.                        Qnil,
  499.                        Qnil);
  500.       unbind_to (speccount, Qnil);
  501.       *stack_trace_displayed = 1;
  502.     }
  503.       
  504.   if (!entering_debugger && !*debugger_entered && !signal_vars_only
  505.       && (EQ (sig, Qquit)
  506.       ? debug_on_quit
  507.       : wants_debugger (Vdebug_on_error, conditions)))
  508.     {
  509.       debug_on_quit &= ~2;    /* reset critical bit */
  510.       specbind (Qdebug_on_error, Qnil);
  511.       specbind (Qstack_trace_on_error, Qnil);
  512.       specbind (Qdebug_on_signal, Qnil);
  513.       specbind (Qstack_trace_on_signal, Qnil);
  514.       
  515.       val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
  516.       *debugger_entered = 1;
  517.     }
  518.  
  519.   if (!entering_debugger && !*stack_trace_displayed
  520.       && wants_debugger (Vstack_trace_on_signal, conditions))
  521.     {
  522.       specbind (Qdebug_on_error, Qnil);
  523.       specbind (Qstack_trace_on_error, Qnil);
  524.       specbind (Qdebug_on_signal, Qnil);
  525.       specbind (Qstack_trace_on_signal, Qnil);
  526.       
  527.       internal_with_output_to_temp_buffer ("*Backtrace*",
  528.                        backtrace_259,
  529.                        Qnil,
  530.                        Qnil);
  531.       unbind_to (speccount, Qnil);
  532.       *stack_trace_displayed = 1;
  533.     }
  534.  
  535.   if (!entering_debugger && !*debugger_entered
  536.       && (EQ (sig, Qquit)
  537.       ? debug_on_quit
  538.       : wants_debugger (Vdebug_on_signal, conditions)))
  539.     {
  540.       debug_on_quit &= ~2;    /* reset critical bit */
  541.       specbind (Qdebug_on_error, Qnil);
  542.       specbind (Qstack_trace_on_error, Qnil);
  543.       specbind (Qdebug_on_signal, Qnil);
  544.       specbind (Qstack_trace_on_signal, Qnil);
  545.       
  546.       val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
  547.       *debugger_entered = 1;
  548.     }
  549.  
  550.   UNGCPRO;
  551.   Vcondition_handlers = all_handlers;
  552.   return (unbind_to (speccount, val));
  553. }
  554.  
  555.  
  556. /**********************************************************************/
  557. /*                     The basic special forms                        */
  558. /**********************************************************************/
  559.  
  560. /* NOTE!!! Every function that can call EVAL must protect its args
  561.    and temporaries from garbage collection while it needs them.
  562.    The definition of `For' shows what you have to do.  */
  563.  
  564. DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
  565.   "Eval args until one of them yields non-nil, then return that value.\n\
  566. The remaining args are not evalled at all.\n\
  567. If all args return nil, return nil.")
  568.   (args)
  569.      Lisp_Object args;
  570. {
  571.   /* This function can GC */
  572.   REGISTER Lisp_Object val;
  573.   Lisp_Object args_left;
  574.   struct gcpro gcpro1;
  575.  
  576.   if (NILP (args))
  577.     return Qnil;
  578.  
  579.   args_left = args;
  580.   GCPRO1 (args_left);
  581.  
  582.   do
  583.     {
  584.       val = Feval (Fcar (args_left));
  585.       if (!NILP (val))
  586.     break;
  587.       args_left = Fcdr (args_left);
  588.     }
  589.   while (!NILP (args_left));
  590.  
  591.   UNGCPRO;
  592.   return val;
  593. }
  594.  
  595. DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
  596.   "Eval args until one of them yields nil, then return nil.\n\
  597. The remaining args are not evalled at all.\n\
  598. If no arg yields nil, return the last arg's value.")
  599.   (args)
  600.      Lisp_Object args;
  601. {
  602.   /* This function can GC */
  603.   REGISTER Lisp_Object val;
  604.   Lisp_Object args_left;
  605.   struct gcpro gcpro1;
  606.  
  607.   if (NILP (args))
  608.     return Qt;
  609.  
  610.   args_left = args;
  611.   GCPRO1 (args_left);
  612.  
  613.   do
  614.     {
  615.       val = Feval (Fcar (args_left));
  616.       if (NILP (val))
  617.     break;
  618.       args_left = Fcdr (args_left);
  619.     }
  620.   while (!NILP (args_left));
  621.  
  622.   UNGCPRO;
  623.   return val;
  624. }
  625.  
  626. DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
  627.   "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
  628. Returns the value of THEN or the value of the last of the ELSE's.\n\
  629. THEN must be one expression, but ELSE... can be zero or more expressions.\n\
  630. If COND yields nil, and there are no ELSE's, the value is nil.")
  631.   (args)
  632.      Lisp_Object args;
  633. {
  634.   /* This function can GC */
  635.   Lisp_Object cond;
  636.   struct gcpro gcpro1;
  637.  
  638.   GCPRO1 (args);
  639.   cond = Feval (Fcar (args));
  640.   UNGCPRO;
  641.  
  642.   if (!NILP (cond))
  643.     return Feval (Fcar (Fcdr (args)));
  644.   return Fprogn (Fcdr (Fcdr (args)));
  645. }
  646.  
  647. DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
  648.   "(cond CLAUSES...): try each clause until one succeeds.\n\
  649. Each clause looks like (CONDITION BODY...).  CONDITION is evaluated\n\
  650. and, if the value is non-nil, this clause succeeds:\n\
  651. then the expressions in BODY are evaluated and the last one's\n\
  652. value is the value of the cond-form.\n\
  653. If no clause succeeds, cond returns nil.\n\
  654. If a clause has one element, as in (CONDITION),\n\
  655. CONDITION's value if non-nil is returned from the cond-form.")
  656.   (args)
  657.      Lisp_Object args;
  658. {
  659.   /* This function can GC */
  660.   REGISTER Lisp_Object clause, val;
  661.   struct gcpro gcpro1;
  662.  
  663.   val = Qnil;
  664.   GCPRO1 (args);
  665.   while (!NILP (args))
  666.     {
  667.       clause = Fcar (args);
  668.       val = Feval (Fcar (clause));
  669.       if (!NILP (val))
  670.     {
  671.       if (!EQ (XCDR (clause), Qnil))
  672.         val = Fprogn (XCDR (clause));
  673.       break;
  674.     }
  675.       args = XCDR (args);
  676.     }
  677.   UNGCPRO;
  678.  
  679.   return val;
  680. }
  681.  
  682. DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
  683.   "(progn BODY...): eval BODY forms sequentially and return value of last one.")
  684.   (args)
  685.      Lisp_Object args;
  686. {
  687.   /* This function can GC */
  688.   REGISTER Lisp_Object val;
  689.   Lisp_Object args_left;
  690.   struct gcpro gcpro1;
  691.  
  692. #ifdef MOCKLISP_SUPPORT
  693.   /* In Mucklisp code, symbols at the front of the progn arglist
  694.    are to be bound to zero. */
  695.   if (!EQ (Vmocklisp_arguments, Qt))
  696.     {
  697.       Lisp_Object tem;
  698.       val = Qzero;
  699.       while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
  700.     {
  701.       QUIT;
  702.       specbind (tem, val), args = Fcdr (args);
  703.     }
  704.     }
  705. #endif
  706.  
  707.   if (NILP (args))
  708.     return Qnil;
  709.  
  710.   args_left = args;
  711.   GCPRO1 (args_left);
  712.  
  713.   do
  714.     {
  715.       val = Feval (Fcar (args_left));
  716.       args_left = Fcdr (args_left);
  717.     }
  718.   while (!NILP (args_left));
  719.  
  720.   UNGCPRO;
  721.   return val;
  722. }
  723.  
  724. DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
  725.   "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
  726. The value of FIRST is saved during the evaluation of the remaining args,\n\
  727. whose values are discarded.")
  728.   (args)
  729.      Lisp_Object args;
  730. {
  731.   /* This function can GC */
  732.   Lisp_Object val;
  733.   REGISTER Lisp_Object args_left;
  734.   struct gcpro gcpro1, gcpro2;
  735.   REGISTER int argnum = 0;
  736.  
  737.   if (NILP (args))
  738.     return Qnil;
  739.  
  740.   args_left = args;
  741.   val = Qnil;
  742.   GCPRO2 (args, val);
  743.  
  744.   do
  745.     {
  746.       if (!(argnum++))
  747.         val = Feval (Fcar (args_left));
  748.       else
  749.     Feval (Fcar (args_left));
  750.       args_left = Fcdr (args_left);
  751.     }
  752.   while (!NILP (args_left));
  753.  
  754.   UNGCPRO;
  755.   return val;
  756. }
  757.  
  758. DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
  759.   "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
  760. The value of Y is saved during the evaluation of the remaining args,\n\
  761. whose values are discarded.")
  762.   (args)
  763.      Lisp_Object args;
  764. {
  765.   /* This function can GC */
  766.   Lisp_Object val;
  767.   REGISTER Lisp_Object args_left;
  768.   struct gcpro gcpro1, gcpro2;
  769.   REGISTER int argnum = -1;
  770.  
  771.   val = Qnil;
  772.  
  773.   if (NILP (args))
  774.     return Qnil;
  775.  
  776.   args_left = args;
  777.   val = Qnil;
  778.   GCPRO2 (args, val);
  779.  
  780.   do
  781.     {
  782.       if (!(argnum++))
  783.         val = Feval (Fcar (args_left));
  784.       else
  785.     Feval (Fcar (args_left));
  786.       args_left = Fcdr (args_left);
  787.     }
  788.   while (!NILP (args_left));
  789.  
  790.   UNGCPRO;
  791.   return val;
  792. }
  793.  
  794. DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
  795.   "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
  796. The value of the last form in BODY is returned.\n\
  797. Each element of VARLIST is a symbol (which is bound to nil)\n\
  798. or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
  799. Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
  800.   (args)
  801.      Lisp_Object args;
  802. {
  803.   /* This function can GC */
  804.   Lisp_Object varlist, val, elt;
  805.   int speccount = specpdl_depth_counter;
  806.   struct gcpro gcpro1, gcpro2, gcpro3;
  807.  
  808.   GCPRO3 (args, elt, varlist);
  809.  
  810.   varlist = Fcar (args);
  811.   while (!NILP (varlist))
  812.     {
  813.       QUIT;
  814.       elt = Fcar (varlist);
  815.       if (SYMBOLP (elt))
  816.     specbind (elt, Qnil);
  817.       else if (! NILP (Fcdr (Fcdr (elt))))
  818.     signal_simple_error ("`let' bindings can have only one value-form",
  819.                              elt);
  820.       else
  821.     {
  822.       val = Feval (Fcar (Fcdr (elt)));
  823.       specbind (Fcar (elt), val);
  824.     }
  825.       varlist = Fcdr (varlist);
  826.     }
  827.   UNGCPRO;
  828.   val = Fprogn (Fcdr (args));
  829.   return unbind_to (speccount, val);
  830. }
  831.  
  832. DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
  833.   "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
  834. The value of the last form in BODY is returned.\n\
  835. Each element of VARLIST is a symbol (which is bound to nil)\n\
  836. or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
  837. All the VALUEFORMs are evalled before any symbols are bound.")
  838.   (args)
  839.      Lisp_Object args;
  840. {
  841.   /* This function can GC */
  842.   Lisp_Object *temps, tem;
  843.   REGISTER Lisp_Object elt, varlist;
  844.   int speccount = specpdl_depth_counter;
  845.   REGISTER int argnum;
  846.   struct gcpro gcpro1, gcpro2;
  847.  
  848.   varlist = Fcar (args);
  849.  
  850.   /* Make space to hold the values to give the bound variables */
  851.   elt = Flength (varlist);
  852.   temps = (Lisp_Object *) alloca (XINT (elt) * sizeof (Lisp_Object));
  853.  
  854.   /* Compute the values and store them in `temps' */
  855.  
  856.   GCPRO2 (args, *temps);
  857.   gcpro2.nvars = 0;
  858.  
  859.   for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
  860.     {
  861.       QUIT;
  862.       elt = Fcar (varlist);
  863.       if (SYMBOLP (elt))
  864.     temps [argnum++] = Qnil;
  865.       else if (! NILP (Fcdr (Fcdr (elt))))
  866.     signal_simple_error ("`let' bindings can have only one value-form",
  867.                              elt);
  868.       else
  869.     temps [argnum++] = Feval (Fcar (Fcdr (elt)));
  870.       gcpro2.nvars = argnum;
  871.     }
  872.   UNGCPRO;
  873.  
  874.   varlist = Fcar (args);
  875.   for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
  876.     {
  877.       elt = Fcar (varlist);
  878.       tem = temps[argnum++];
  879.       if (SYMBOLP (elt))
  880.     specbind (elt, tem);
  881.       else
  882.     specbind (Fcar (elt), tem);
  883.     }
  884.  
  885.   elt = Fprogn (Fcdr (args));
  886.   return unbind_to (speccount, elt);
  887. }
  888.  
  889. DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
  890.   "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
  891. The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
  892. until TEST returns nil.")
  893.   (args)
  894.      Lisp_Object args;
  895. {
  896.   /* This function can GC */
  897.   Lisp_Object test, body, tem;
  898.   struct gcpro gcpro1, gcpro2;
  899.  
  900.   GCPRO2 (test, body);
  901.  
  902.   test = Fcar (args);
  903.   body = Fcdr (args);
  904.   while (tem = Feval (test), !NILP (tem))
  905.     {
  906.       QUIT;
  907.       Fprogn (body);
  908.     }
  909.  
  910.   UNGCPRO;
  911.   return Qnil;
  912. }
  913.  
  914. Lisp_Object Qsetq;
  915.  
  916. DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
  917.   "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
  918. The SYMs are not evaluated.  Thus (setq x y) sets x to the value of y.\n\
  919. Each SYM is set before the next VAL is computed.")
  920.   (args)
  921.      Lisp_Object args;
  922. {
  923.   /* This function can GC */
  924.   REGISTER Lisp_Object args_left;
  925.   REGISTER Lisp_Object val, sym;
  926.   struct gcpro gcpro1;
  927.  
  928.   if (NILP (args))
  929.     return Qnil;
  930.  
  931.   val = Flength (args);
  932.   if (XINT (val) & 1 != 0)
  933.     Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, val));
  934.  
  935.   args_left = args;
  936.   GCPRO1 (args);
  937.  
  938.   do
  939.     {
  940.       val = Feval (Fcar (Fcdr (args_left)));
  941.       sym = Fcar (args_left);
  942.       Fset (sym, val);
  943.       args_left = Fcdr (Fcdr (args_left));
  944.     }
  945.   while (!NILP (args_left));
  946.  
  947.   UNGCPRO;
  948.   return val;
  949. }
  950.      
  951. DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
  952.   "Return the argument, without evaluating it.  `(quote x)' yields `x'.")
  953.   (args)
  954.      Lisp_Object args;
  955. {
  956.   return Fcar (args);
  957. }
  958.      
  959. DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
  960.   "Like `quote', but preferred for objects which are functions.\n\
  961. In byte compilation, `function' causes its argument to be compiled.\n\
  962. `quote' cannot do that.")
  963.   (args)
  964.      Lisp_Object args;
  965. {
  966.   return Fcar (args);
  967. }
  968.  
  969.  
  970. /**********************************************************************/
  971. /*                     Defining functions/variables                   */
  972. /**********************************************************************/
  973.  
  974. DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
  975.   "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
  976. The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
  977. See also the function `interactive'.")
  978.   (args)
  979.      Lisp_Object args;
  980. {
  981.   /* This function can GC */
  982.   Lisp_Object fn_name;
  983.   Lisp_Object defn;
  984.  
  985.   fn_name = Fcar (args);
  986.   defn = Fcons (Qlambda, Fcdr (args));
  987.   if (purify_flag)
  988.     defn = Fpurecopy (defn);
  989.   Ffset (fn_name, defn);
  990.   LOADHIST_ATTACH (fn_name);
  991.   return fn_name;
  992. }
  993.  
  994. DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
  995.   "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
  996. The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
  997. When the macro is called, as in (NAME ARGS...),\n\
  998. the function (lambda ARGLIST BODY...) is applied to\n\
  999. the list ARGS... as it appears in the expression,\n\
  1000. and the result should be a form to be evaluated instead of the original.")
  1001.   (args)
  1002.      Lisp_Object args;
  1003. {
  1004.   /* This function can GC */
  1005.   Lisp_Object fn_name;
  1006.   Lisp_Object defn;
  1007.  
  1008.   fn_name = Fcar (args);
  1009.   defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
  1010.   if (purify_flag)
  1011.     defn = Fpurecopy (defn);
  1012.   Ffset (fn_name, defn);
  1013.   LOADHIST_ATTACH (fn_name);
  1014.   return fn_name;
  1015. }
  1016.  
  1017. DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
  1018.   "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
  1019. You are not required to define a variable in order to use it,\n\
  1020.  but the definition can supply documentation and an initial value\n\
  1021.  in a way that tags can recognize.\n\n\
  1022. INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is\n\
  1023.  void. (However, when you evaluate a defvar interactively, it acts like a\n\
  1024.  defconst: SYMBOL's value is always set regardless of whether it's currently\n\
  1025.  void.)\n\
  1026. If SYMBOL is buffer-local, its default value is what is set;\n\
  1027.  buffer-local values are not affected.\n\
  1028. INITVALUE and DOCSTRING are optional.\n\
  1029. If DOCSTRING starts with *, this variable is identified as a user option.\n\
  1030.  This means that M-x set-variable and M-x edit-options recognize it.\n\
  1031. If INITVALUE is missing, SYMBOL's value is not set.\n\
  1032. \n\
  1033. In lisp-interaction-mode defvar is treated as defconst.")
  1034.   (args)
  1035.      Lisp_Object args;
  1036. {
  1037.   /* This function can GC */
  1038.   REGISTER Lisp_Object sym, tem;
  1039.  
  1040.   sym = Fcar (args);
  1041.   tem = Fcdr (args);
  1042.   if (!NILP (tem))
  1043.     {
  1044.       tem = Fdefault_boundp (sym);
  1045.       if (NILP (tem))
  1046.     Fset_default (sym, Feval (Fcar (Fcdr (args))));
  1047.     }
  1048.  
  1049. #ifdef I18N3
  1050.   if (!NILP (Vfile_domain))
  1051.     pure_put (sym, Qvariable_domain, Vfile_domain);
  1052. #endif
  1053.  
  1054.   tem = Fcar (Fcdr (Fcdr (args)));
  1055.   if (!NILP (tem))
  1056.     pure_put (sym, Qvariable_documentation, tem);
  1057.  
  1058.   LOADHIST_ATTACH (sym);
  1059.   return sym;
  1060. }
  1061.  
  1062. DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
  1063.   "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant\n\
  1064. variable.\n\
  1065. The intent is that programs do not change this value, but users may.\n\
  1066. Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
  1067. If SYMBOL is buffer-local, its default value is what is set;\n\
  1068.  buffer-local values are not affected.\n\
  1069. DOCSTRING is optional.\n\
  1070. If DOCSTRING starts with *, this variable is identified as a user option.\n\
  1071.  This means that M-x set-variable and M-x edit-options recognize it.\n\n\
  1072. Note: do not use `defconst' for user options in libraries that are not\n\
  1073.  normally loaded, since it is useful for users to be able to specify\n\
  1074.  their own values for such variables before loading the library.\n\
  1075. Since `defconst' unconditionally assigns the variable,\n\
  1076.  it would override the user's choice.")
  1077.   (args)
  1078.      Lisp_Object args;
  1079. {
  1080.   /* This function can GC */
  1081.   REGISTER Lisp_Object sym, tem;
  1082.  
  1083.   sym = Fcar (args);
  1084.   Fset_default (sym, Feval (Fcar (Fcdr (args))));
  1085.  
  1086. #ifdef I18N3
  1087.   if (!NILP (Vfile_domain))
  1088.     pure_put (sym, Qvariable_domain, Vfile_domain);
  1089. #endif
  1090.  
  1091.   tem = Fcar (Fcdr (Fcdr (args)));
  1092.  
  1093.   if (!NILP (tem))
  1094.     pure_put (sym, Qvariable_documentation, tem);
  1095.  
  1096.   LOADHIST_ATTACH (sym);
  1097.   return sym;
  1098. }
  1099.  
  1100. DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
  1101.   "Return t if VARIABLE is intended to be set and modified by users.\n\
  1102. \(The alternative is a variable used internally in a Lisp program.)\n\
  1103. Determined by whether the first character of the documentation\n\
  1104. for the variable is \"*\"")
  1105.   (variable)
  1106.      Lisp_Object variable;
  1107. {
  1108.   Lisp_Object documentation;
  1109.   
  1110.   documentation = Fget (variable, Qvariable_documentation, Qnil);
  1111.   if (INTP (documentation) && XINT (documentation) < 0)
  1112.     return Qt;
  1113.   if ((STRINGP (documentation)) &&
  1114.       (string_byte (XSTRING (documentation), 0) == '*'))
  1115.     return Qt;
  1116.   return Qnil;
  1117. }  
  1118.  
  1119. DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
  1120.   "Return result of expanding macros at top level of FORM.\n\
  1121. If FORM is not a macro call, it is returned unchanged.\n\
  1122. Otherwise, the macro is expanded and the expansion is considered\n\
  1123. in place of FORM.  When a non-macro-call results, it is returned.\n\n\
  1124. The second optional arg ENVIRONMENT species an environment of macro\n\
  1125. definitions to shadow the loaded ones for use in file byte-compilation.")
  1126.   (form, env)
  1127.      Lisp_Object form;
  1128.      Lisp_Object env;
  1129. {
  1130.   /* This function can GC */
  1131.   /* With cleanups from Hallvard Furuseth.  */
  1132.   REGISTER Lisp_Object expander, sym, def, tem;
  1133.  
  1134.   for (;;)
  1135.     {
  1136.       /* Come back here each time we expand a macro call,
  1137.      in case it expands into another macro call.  */
  1138.       if (!CONSP (form))
  1139.     break;
  1140.       /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
  1141.       def = sym = XCAR (form);
  1142.       tem = Qnil;
  1143.       /* Trace symbols aliases to other symbols
  1144.      until we get a symbol that is not an alias.  */
  1145.       while (SYMBOLP (def))
  1146.     {
  1147.       QUIT;
  1148.       sym = def;
  1149.       tem = Fassq (sym, env);
  1150.       if (NILP (tem))
  1151.         {
  1152.           def = XSYMBOL (sym)->function;
  1153.           if (!EQ (def, Qunbound))
  1154.         continue;
  1155.         }
  1156.       break;
  1157.     }
  1158.       /* Right now TEM is the result from SYM in ENV,
  1159.      and if TEM is nil then DEF is SYM's function definition.  */
  1160.       if (!NILP (tem))
  1161.     {
  1162.       expander = XCDR (tem);
  1163.       if (NILP (expander))
  1164.         break;
  1165.     }
  1166.       else
  1167.     {
  1168.       /* SYM is not mentioned in ENV.
  1169.          Look at its function definition.  */
  1170.       if (EQ (def, Qunbound)
  1171.           || !CONSP (def))
  1172.         /* Not defined or definition not suitable */
  1173.         break;
  1174.       if (EQ (XCAR (def), Qautoload))
  1175.         {
  1176.           /* Autoloading function: will it be a macro when loaded?  */
  1177.           tem = Felt (def, make_number (4));
  1178.           if (EQ (tem, Qt) || EQ (tem, Qmacro))
  1179.         {
  1180.           /* Yes, load it and try again.  */
  1181.           do_autoload (def, sym);
  1182.           continue;
  1183.         }
  1184.           else
  1185.         break;
  1186.         }
  1187.       else if (!EQ (XCAR (def), Qmacro))
  1188.         break;
  1189.       else expander = XCDR (def);
  1190.     }
  1191.       form = apply1 (expander, XCDR (form));
  1192.     }
  1193.   return form;
  1194. }
  1195.  
  1196.  
  1197. /**********************************************************************/
  1198. /*                          Non-local exits                           */
  1199. /**********************************************************************/
  1200.  
  1201. DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
  1202.   "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
  1203. TAG is evalled to get the tag to use.  Then the BODY is executed.\n\
  1204. Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
  1205. If no throw happens, `catch' returns the value of the last BODY form.\n\
  1206. If a throw happens, it specifies the value to return from `catch'.")
  1207.   (args)
  1208.      Lisp_Object args;
  1209. {
  1210.   /* This function can GC */
  1211.   Lisp_Object tag;
  1212.   struct gcpro gcpro1;
  1213.  
  1214.   GCPRO1 (args);
  1215.   tag = Feval (Fcar (args));
  1216.   UNGCPRO;
  1217.   return internal_catch (tag, Fprogn, Fcdr (args), 0);
  1218. }
  1219.  
  1220. /* Set up a catch, then call C function FUNC on argument ARG.
  1221.    FUNC should return a Lisp_Object.
  1222.    This is how catches are done from within C code. */
  1223.  
  1224. Lisp_Object
  1225. internal_catch (Lisp_Object tag, 
  1226.                 Lisp_Object (*func) (Lisp_Object arg),
  1227.                 Lisp_Object arg,
  1228.                 int *threw)
  1229. {
  1230.   /* This structure is made part of the chain `catchlist'.  */
  1231.   struct catchtag c;
  1232.  
  1233.   /* Fill in the components of c, and put it on the list.  */
  1234.   c.next = catchlist;
  1235.   c.tag = tag;
  1236.   c.val = Qnil;
  1237.   c.backlist = backtrace_list;
  1238.   c.lisp_eval_depth = lisp_eval_depth;
  1239.   c.pdlcount = specpdl_depth_counter;
  1240.   c.gcpro = gcprolist;
  1241.   catchlist = &c;
  1242.  
  1243.   /* Call FUNC.  */
  1244.   if (setjmp (c.jmp))
  1245.     {
  1246.       /* Throw works by a longjmp that comes right here.  */
  1247.       if (threw) *threw = 1;
  1248.       return (c.val);
  1249.     }
  1250.   c.val = (*func) (arg);
  1251.   if (threw) *threw = 0;
  1252.   catchlist = c.next;
  1253.   return (c.val);
  1254. }
  1255.  
  1256. static Lisp_Object /* ha ha! */
  1257. throw_or_bomb_out (Lisp_Object tag, Lisp_Object val, int bomb_out_p,
  1258.            Lisp_Object sig, Lisp_Object data)
  1259. {
  1260.   /* die if we recurse more than is reasonable */
  1261.   if (++throw_level > 20)
  1262.     abort();
  1263.  
  1264.   /* If bomb_out_p is t, this is being called from Fsignal as a
  1265.      "last resort" when there is no handler for this error and
  1266.       the debugger couldn't be invoked, so we are throwing to
  1267.      'top-level.  If this tag doesn't exist (happens during the
  1268.      initialization stages) we would get in an infinite recursive
  1269.      Fsignal/Fthrow loop, so instead we bomb out to the
  1270.      really-early-error-handler.
  1271.  
  1272.      Note that in fact the only time that the "last resort"
  1273.      occurs is when there's no catch for 'top-level -- the
  1274.      'top-level catch and the catch-all error handler are
  1275.      established at the same time, in initial_command_loop/
  1276.      top_level_1.
  1277.  
  1278.      #### Fix this horrifitude! 
  1279.      */
  1280.      
  1281.   for (;;)
  1282.     {
  1283.       struct catchtag *c;
  1284.  
  1285.       for (c = catchlist; c; c = c->next)
  1286.     {
  1287.       if (EQ (c->tag, tag))
  1288.             {
  1289.               /* Unwind the specbind, catch, and handler stacks back to CATCH
  1290.                  Before each catch is discarded, unbind all special bindings
  1291.                  and execute all unwind-protect clauses made above that catch.
  1292.                  At the end, restore some static info saved in CATCH,
  1293.                  and longjmp to the location specified.
  1294.                  */
  1295.  
  1296.               /* Save the value somewhere it will be GC'ed.
  1297.                  (Can't overwrite tag slot because an unwind-protect may 
  1298.                  want to throw to this same tag, which isn't yet invalid.) */
  1299.               c->val = val;
  1300.               /* Unwind the specpdl stack */
  1301.               unbind_to (c->pdlcount, Qnil);
  1302.               gcprolist = c->gcpro;
  1303.               backtrace_list = c->backlist;
  1304.               lisp_eval_depth = c->lisp_eval_depth;
  1305.               catchlist = c->next;
  1306.           throw_level = 0;
  1307.               longjmp (c->jmp, 1);
  1308.             } 
  1309.     }
  1310.       if (!bomb_out_p)
  1311.         tag = Fsignal (Qno_catch, list2 (tag, val));
  1312.       else
  1313.         call1 (Qreally_early_error_handler, Fcons (sig, data));
  1314.     }
  1315.  
  1316.   /* can't happen.  who cares? */
  1317.   throw_level--;
  1318.   /* getting tired of compilation warnings */
  1319.   return Qnil;
  1320. }
  1321.  
  1322. /* See above, where CATCHLIST is defined, for a description of how
  1323.    Fthrow() works.
  1324.  
  1325.    Fthrow() is also called by Fsignal(), to do a non-local jump
  1326.    back to the appropriate condition-case handler after (maybe)
  1327.    the debugger is entered.  In that case, TAG is the value
  1328.    of Vcondition_handlers that was in place just after the
  1329.    condition-case handler was set up.  The car of this will be
  1330.    some data referring to the handler: Its car will be Qunbound
  1331.    (thus, this tag can never be generated by Lisp code), and
  1332.    its CDR will be the HANDLERS argument to condition_case_1()
  1333.    (either Qerror, Qt, or a list of handlers as in `condition-case').
  1334.    This works fine because Fthrow() does not care what TAG was
  1335.    passed to it: it just looks up the catch list for something
  1336.    that is EQ() to TAG.  When it finds it, it will longjmp()
  1337.    back to the place that established the catch (in this case,
  1338.    condition_case_1).  See below for more info.
  1339. */
  1340.  
  1341. DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
  1342.   "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
  1343. Both TAG and VALUE are evalled.")
  1344.   (tag, val)
  1345.      Lisp_Object tag, val;
  1346. {
  1347.   return throw_or_bomb_out (tag, val, 0, Qnil, Qnil);
  1348. }
  1349.  
  1350. DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
  1351.   "Do BODYFORM, protecting with UNWINDFORMS.\n\
  1352. Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
  1353. If BODYFORM completes normally, its value is returned\n\
  1354. after executing the UNWINDFORMS.\n\
  1355. If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
  1356.   (args)
  1357.      Lisp_Object args;
  1358. {
  1359.   /* This function can GC */
  1360.   Lisp_Object val;
  1361.   int speccount = specpdl_depth_counter;
  1362.  
  1363.   record_unwind_protect (Fprogn, Fcdr (args));
  1364.   val = Feval (Fcar (args));
  1365.   return unbind_to (speccount, val);
  1366. }
  1367.  
  1368.  
  1369. /**********************************************************************/
  1370. /*                    Signalling and trapping errors                  */
  1371. /**********************************************************************/
  1372.  
  1373. static Lisp_Object
  1374. condition_bind_unwind (Lisp_Object loser)
  1375. {
  1376.   struct Lisp_Cons *victim;
  1377.   /* ((handler-fun . handler-args) ... other handlers) */
  1378.   Lisp_Object tem = XCAR (loser);
  1379.  
  1380.   while (CONSP (tem))
  1381.     {
  1382.       victim = XCONS (tem);
  1383.       tem = victim->cdr;
  1384.       free_cons (victim);
  1385.     }
  1386.   victim = XCONS (loser);
  1387.  
  1388.   if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
  1389.     Vcondition_handlers = victim->cdr;
  1390.  
  1391.   free_cons (victim);
  1392.   return (Qnil);
  1393. }
  1394.  
  1395. static Lisp_Object
  1396. condition_case_unwind (Lisp_Object loser)
  1397. {
  1398.   struct Lisp_Cons *victim;
  1399.  
  1400.   /* ((<unbound> . clauses) ... other handlers */
  1401.   victim = XCONS (XCAR (loser));
  1402.   free_cons (victim);
  1403.  
  1404.   victim = XCONS (loser);
  1405.   if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
  1406.     Vcondition_handlers = victim->cdr;
  1407.  
  1408.   free_cons (victim);
  1409.   return (Qnil);
  1410. }
  1411.  
  1412. /* Split out from condition_case_3 so that primitive C callers
  1413.    don't have to cons up a lisp handler form to be evaluated. */
  1414.  
  1415. /* Call a function BFUN of one argument BARG, trapping errors as
  1416.    specified by HANDLERS.  If no error occurs that is indicated by
  1417.    HANDLERS as something to be caught, the return value of this
  1418.    function is the return value from BFUN.  If such an error does
  1419.    occur, HFUN is called, and its return value becomes the
  1420.    return value of condition_case_1().  The second argument passed
  1421.    to HFUN will always be HARG.  The first argument depends on
  1422.    HANDLERS:
  1423.  
  1424.    If HANDLERS is Qt, all errors (this includes QUIT, but not
  1425.    non-local exits with `throw') cause HFUN to be invoked, and VAL
  1426.    (the first argument to HFUN) is a cons (SIG . DATA) of the
  1427.    arguments passed to `signal'.  The debugger is not invoked even if
  1428.    `debug-on-error' was set.
  1429.  
  1430.    A HANDLERS value of Qerror is the same as Qt except that the
  1431.    debugger is invoked if `debug-on-error' was set.
  1432.  
  1433.    Otherwise, HANDLERS should be a list of lists (CONDITION-NAME BODY ...)
  1434.    exactly as in `condition-case', and errors will be trapped
  1435.    as indicated in HANDLERS.  VAL (the first argument to HFUN) will
  1436.    be a cons whose car is the cons (SIG . DATA) and whose CDR is the
  1437.    list (BODY ...) from the appropriate slot in HANDLERS.
  1438.  
  1439.    This function pushes HANDLERS onto the front of Vcondition_handlers
  1440.    (actually with a Qunbound marker as well -- see Fthrow() above
  1441.    for why), establishes a catch whose tag is this new value of
  1442.    Vcondition_handlers, and calls BFUN.  When Fsignal() is called,
  1443.    it calls Fthrow(), setting TAG to this same new value of
  1444.    Vcondition_handlers and setting VAL to the same thing that will
  1445.    be passed to HFUN, as above.  Fthrow() longjmp()s back to the
  1446.    jump point we just established, and we in turn just call the
  1447.    HFUN and return its value.
  1448.  
  1449.    For a real condition-case, HFUN will always be
  1450.    run_condition_case_handlers() and HARG is the argument VAR
  1451.    to condition-case.  That function just binds VAR to the cons
  1452.    (SIG . DATA) that is the CAR of VAL, and calls the handler
  1453.    (BODY ...) that is the CDR of VAL.  Note that before calling
  1454.    Fthrow(), Fsignal() restored Vcondition_handlers to the value
  1455.    it had *before* condition_case_1() was called.  This maintains
  1456.    consistency (so that the state of things at exit of
  1457.    condition_case_1() is the same as at entry), and implies
  1458.    that the handler can signal the same error again (possibly
  1459.    after processing of its own), without getting in an infinite
  1460.    loop. */
  1461.  
  1462. Lisp_Object
  1463. condition_case_1 (Lisp_Object handlers,
  1464.                   Lisp_Object (*bfun) (Lisp_Object barg),
  1465.                   Lisp_Object barg,
  1466.                   Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg),
  1467.                   Lisp_Object harg)
  1468. {
  1469.   int speccount = specpdl_depth_counter;
  1470.   struct catchtag c;
  1471.   struct gcpro gcpro1;
  1472.  
  1473.   /* Do consing now so out-of-memory error happens up front */
  1474.   /* (unbound . stuff) is a special condition-case kludge marker
  1475.      which is known specially by Fsignal.
  1476.      This is an abomination, but to fix it would require either
  1477.      making condition_case cons (a union of the conditions of the clauses)
  1478.      or changing the byte-compiler output (no thanks). */
  1479.   c.tag = Fcons (Fcons (Qunbound, handlers), Vcondition_handlers);
  1480.   c.val = Qnil;
  1481.   c.backlist = backtrace_list;
  1482.   c.lisp_eval_depth = lisp_eval_depth;
  1483.   c.pdlcount = specpdl_depth_counter;
  1484.   c.gcpro = gcprolist;
  1485.   c.next = catchlist;
  1486.  
  1487.   if (setjmp (c.jmp))
  1488.     {
  1489.       /* throw does ungcpro, etc */
  1490.       return ((*hfun) (c.val, harg));
  1491.     }
  1492.  
  1493.   record_unwind_protect (condition_case_unwind, c.tag);
  1494.  
  1495.   Vcondition_handlers = c.tag;
  1496.   catchlist = &c;
  1497.   GCPRO1 (harg);                /* Somebody has to gc-protect */
  1498.  
  1499.   c.val = ((*bfun) (barg));
  1500.  
  1501.   /* The following is *not* true: (ben)
  1502.  
  1503.      ungcpro, restoring catchlist and condition_handlers are actually
  1504.      redundant since unbind_to now restores them.  But it looks funny not to
  1505.      have this code here, and it doesn't cost anything, so I'm leaving it.*/
  1506.   UNGCPRO;
  1507.   catchlist = c.next;
  1508.   Vcondition_handlers = XCDR (c.tag);
  1509.  
  1510.   return (unbind_to (speccount, c.val));
  1511. }
  1512.  
  1513. static Lisp_Object
  1514. run_condition_case_handlers (Lisp_Object val, Lisp_Object var)
  1515. {
  1516.   /* This function can GC */
  1517.   int speccount;
  1518.  
  1519.   if (NILP (var))
  1520.     return (Fprogn (Fcdr (val))); /* tailcall */
  1521.  
  1522.   speccount = specpdl_depth_counter;
  1523.   specbind (var, Fcar (val));
  1524.   val = Fprogn (Fcdr (val));
  1525.   return unbind_to (speccount, val);
  1526. }
  1527.  
  1528. /* Here for bytecode to call non-consfully.  This is exactly like
  1529.    condition-case except that it takes three arguments rather
  1530.    than a single list of arguments. */
  1531. Lisp_Object
  1532. Fcondition_case_3 (Lisp_Object bodyform, 
  1533.                    Lisp_Object var, Lisp_Object handlers)
  1534. {
  1535.   /* This function can GC */
  1536.   Lisp_Object val;
  1537.  
  1538.   CHECK_SYMBOL (var, 0);
  1539.  
  1540.   for (val = handlers; ! NILP (val); val = Fcdr (val))
  1541.     {
  1542.       Lisp_Object tem;
  1543.       tem = Fcar (val);
  1544.       if ((!NILP (tem)) 
  1545.           && (!CONSP (tem)
  1546.           || (!SYMBOLP (XCAR (tem)) && !CONSP (XCAR (tem)))))
  1547.     signal_simple_error ("Invalid condition handler", tem);
  1548.     }
  1549.  
  1550.   return condition_case_1 (handlers, 
  1551.                            Feval, bodyform,
  1552.                            run_condition_case_handlers,
  1553.                            var);
  1554. }
  1555.  
  1556. DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
  1557.   "Regain control when an error is signalled.\n\
  1558. Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
  1559. executes BODYFORM and returns its value if no error happens.\n\
  1560. Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
  1561. where the BODY is made of Lisp expressions.\n\n\
  1562. A handler is applicable to an error if CONDITION-NAME is one of the\n\
  1563. error's condition names.  If an error happens, the first applicable\n\
  1564. handler is run.  As a special case, a CONDITION-NAME of t matches\n\
  1565. all errors, even those without the `error' condition name on them\n\
  1566. (e.g. `quit').\n\
  1567. \n\
  1568. The car of a handler may be a list of condition names\n\
  1569. instead of a single condition name.\n\
  1570. \n\
  1571. When a handler handles an error,\n\
  1572. control returns to the condition-case and the handler BODY... is executed\n\
  1573. with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
  1574. VAR may be nil; then you do not get access to the signal information.\n\
  1575. \n\
  1576. The value of the last BODY form is returned from the condition-case.\n\
  1577. See also the function `signal' for more info.\n\
  1578. \n\
  1579. Note that at the time the condition handler is invoked, the Lisp stack\n\
  1580. and the current catches, condition-cases, and bindings have all been\n\
  1581. popped back to the state they were in just before the call to\n\
  1582. `condition-case'.  This means that resignalling the error from\n\
  1583. within the handler will not result in an infinite loop.\n\
  1584. \n\
  1585. If you want to establish an error handler that is called with the\n\
  1586. Lisp stack, bindings, etc. as they were when `signal' was called,\n\
  1587. rather than when the handler was set, use `call-with-condition-handler'.")
  1588.   (args)
  1589.      Lisp_Object args;
  1590. {
  1591.   /* This function can GC */
  1592.   return Fcondition_case_3 (Fcar (Fcdr (args)),
  1593.                             Fcar (args),
  1594.                             Fcdr (Fcdr (args)));
  1595.  
  1596. DEFUN ("call-with-condition-handler",
  1597.        Fcall_with_condition_handler,
  1598.        Scall_with_condition_handler, 2, MANY, 0,
  1599.   "Regain control when an error is signalled, without popping the stack.\n\
  1600. Usage looks like (call-with-condition-handler HANDLER FUNCTION &rest ARGS).\n\
  1601. This function is similar to `condition-case', but the handler is invoked\n\
  1602. with the same environment (Lisp stack, bindings, catches, condition-cases)\n\
  1603. that was current when `signal' was called, rather than when the handler\n\
  1604. was established.\n\
  1605. \n\
  1606. HANDLER should be a function of one argument, which is a cons of the args\n\
  1607. (SIG . DATA) that were passed to `signal'.  It is invoked whenever\n\
  1608. `signal' is called (this differs from `condition-case', which allows\n\
  1609. you to specify which errors are trapped).  If the handler function\n\
  1610. returns, `signal' continues as if the handler were never invoked.\n\
  1611. (It continues to look for handlers established earlier than this one,\n\
  1612. and invokes the standard error-handler if none is found.)")
  1613.   (nargs, args)                 /* Note!  Args side-effected! */
  1614.      int nargs;
  1615.      Lisp_Object *args;
  1616. {
  1617.   /* This function can GC */
  1618.   int speccount = specpdl_depth_counter;
  1619.   Lisp_Object tem;
  1620.  
  1621.   /* #### If there were a way to check that args[0] were a function
  1622.      which accepted one arg, that should be done here ... */
  1623.  
  1624.   /* (handler-fun . handler-args) */
  1625.   tem =    Fcons (list1 (args[0]), Vcondition_handlers);
  1626.   record_unwind_protect (condition_bind_unwind, tem);
  1627.   Vcondition_handlers = tem;
  1628.  
  1629.   /* Caller should have GC-protected args */
  1630.   tem = Ffuncall (nargs - 1, args + 1);
  1631.   return (unbind_to (speccount, tem));
  1632. }
  1633.  
  1634. static int
  1635. condition_type_p (Lisp_Object type, Lisp_Object conditions)
  1636. {
  1637.   if (EQ (type, Qt))
  1638.     /* (condition-case c # (t c)) catches -all- signals
  1639.      *   Use with caution! */
  1640.     return (1);
  1641.   else
  1642.     {
  1643.       if (SYMBOLP (type))
  1644.     {
  1645.       return (!NILP (Fmemq (type, conditions)));
  1646.     }
  1647.       else if (CONSP (type))
  1648.     {
  1649.       while (CONSP (type))
  1650.         {
  1651.           if (!NILP (Fmemq (Fcar (type), conditions)))
  1652.         return 1;
  1653.           type = XCDR (type);
  1654.         }
  1655.       return 0;
  1656.     }
  1657.       else
  1658.     return 0;
  1659.     }
  1660. }
  1661.  
  1662. static Lisp_Object
  1663. return_from_signal (Lisp_Object value)
  1664. {
  1665. #if 1 /* RMS Claims: */
  1666.   /* Most callers are not prepared to handle gc if this
  1667.      returns.  So, since this feature is not very useful,
  1668.      take it out.  */
  1669.   /* Have called debugger; return value to signaller  */
  1670.   return (value);
  1671. #else  /* But the reality is that that stinks, because: */
  1672.   /* GACK!!! Really want some way for debug-on-quit errors
  1673.      to be continuable!! */
  1674.   error ("Returning a value from an error is no longer supported");
  1675. #endif
  1676. }
  1677.  
  1678. extern int in_display;
  1679. extern int gc_in_progress;
  1680.  
  1681. static Lisp_Object
  1682. signal_1 (Lisp_Object sig, Lisp_Object data)
  1683. {
  1684.   /* This function can GC */
  1685.   struct gcpro gcpro1, gcpro2;
  1686.   Lisp_Object conditions;
  1687.   Lisp_Object handlers;
  1688.   /* signal_call_debugger() could get called more than once
  1689.      (once when a call-with-condition-handler is about to
  1690.      be dealt with, and another when a condition-case handler
  1691.      is about to be invoked).  So make sure the debugger and/or
  1692.      stack trace aren't done more than once. */
  1693.   int stack_trace_displayed = 0;
  1694.   int debugger_entered = 0;
  1695.   GCPRO2 (conditions, handlers);
  1696.  
  1697.   if (!initialized)
  1698.     {
  1699.       /* who knows how much has been initialized?  Safest bet is
  1700.          just to bomb out immediately. */
  1701.       fprintf (stderr, "Error before initialization is complete!\n");
  1702.       abort ();
  1703.     }
  1704.  
  1705.   if (gc_in_progress || in_display)
  1706.     /* This is one of many reasons why you can't run lisp code from redisplay.
  1707.        There is no sensible way to handle errors there. */
  1708.     abort ();
  1709.  
  1710.   conditions = Fget (sig, Qerror_conditions, Qnil);
  1711.  
  1712.   for (handlers = Vcondition_handlers;
  1713.        CONSP (handlers);
  1714.        handlers = XCDR (handlers))
  1715.     {
  1716.       Lisp_Object handler_fun = XCAR (XCAR (handlers));
  1717.       Lisp_Object handler_data = XCDR (XCAR (handlers));
  1718.       Lisp_Object outer_handlers = XCDR (handlers);
  1719.  
  1720.       if (!EQ (handler_fun, Qunbound))
  1721.         {
  1722.           /* call-with-condition-handler */
  1723.           Lisp_Object tem;
  1724.           Lisp_Object all_handlers = Vcondition_handlers;
  1725.           struct gcpro gcpro1;
  1726.           GCPRO1 (all_handlers);
  1727.           Vcondition_handlers = outer_handlers;
  1728.  
  1729.           tem = signal_call_debugger (conditions, sig, data,
  1730.                       outer_handlers, 1,
  1731.                       &stack_trace_displayed,
  1732.                       &debugger_entered);
  1733.           if (!EQ (tem, Qunbound))
  1734.             RETURN_UNGCPRO (return_from_signal (tem));
  1735.  
  1736.           tem = Fcons (sig, data);
  1737.           if (NILP (handler_data))
  1738.             tem = call1 (handler_fun, tem);
  1739.           else
  1740.             {
  1741.               /* (This code won't be used (for now?).) */
  1742.               struct gcpro gcpro1;
  1743.               Lisp_Object args[3];
  1744.               GCPRO1 (args[0]);
  1745.               gcpro1.nvars = 3;
  1746.               args[0] = handler_fun;
  1747.               args[1] = tem;
  1748.               args[2] = handler_data;
  1749.               gcpro1.var = args;
  1750.               tem = Fapply (3, args);
  1751.               UNGCPRO;
  1752.             }
  1753.           UNGCPRO;
  1754. #if 0
  1755.           if (!EQ (tem, Qsignal))
  1756.             return (return_from_signal (tem));
  1757. #endif
  1758.           /* If handler didn't throw, try another handler */
  1759.           Vcondition_handlers = all_handlers;
  1760.         }
  1761.  
  1762.       /* It's a condition-case handler */
  1763.  
  1764.       /* t is used by handlers for all conditions, set up by C code. 
  1765.        *  debugger is not called even if debug_on_error */
  1766.       else if (EQ (handler_data, Qt))
  1767.     {
  1768.           UNGCPRO;
  1769.           return (Fthrow (handlers, Fcons (sig, data)));
  1770.     }
  1771.       /* `error' is used similarly to the way `t' is used, but in
  1772.          addition it invokes the debugger if debug_on_error.
  1773.      This is normally used for the outer command-loop error
  1774.      handler. */
  1775.       else if (EQ (handler_data, Qerror))
  1776.         {
  1777.           Lisp_Object tem = signal_call_debugger (conditions, sig, data,
  1778.                                                   outer_handlers, 0,
  1779.                           &stack_trace_displayed,
  1780.                           &debugger_entered);
  1781.  
  1782.           UNGCPRO;
  1783.           if (!EQ (tem, Qunbound))
  1784.             return (return_from_signal (tem));
  1785.  
  1786.           tem = Fcons (sig, data);
  1787.           return (Fthrow (handlers, tem));
  1788.         }
  1789.       else
  1790.     {
  1791.           /* handler established by real (Lisp) condition-case */
  1792.           Lisp_Object h;
  1793.  
  1794.       for (h = handler_data; CONSP (h); h = Fcdr (h))
  1795.         {
  1796.           Lisp_Object clause = Fcar (h);
  1797.           Lisp_Object tem = Fcar (clause);
  1798.  
  1799.           if (condition_type_p (tem, conditions))
  1800.         {
  1801.           tem = signal_call_debugger (conditions, sig, data,
  1802.                                               outer_handlers, 1,
  1803.                           &stack_trace_displayed,
  1804.                           &debugger_entered);
  1805.                   UNGCPRO;
  1806.           if (!EQ (tem, Qunbound))
  1807.                     return (return_from_signal (tem));
  1808.  
  1809.                   /* Doesn't return */
  1810.                   tem = Fcons (Fcons (sig, data), Fcdr (clause));
  1811.                   return (Fthrow (handlers, tem));
  1812.                 }
  1813.         }
  1814.     }
  1815.     }
  1816.  
  1817.   /* If no handler is present now, try to run the debugger,
  1818.      and if that fails, throw to top level.
  1819.  
  1820.      #### The only time that no handler is present is during
  1821.      temacs or perhaps very early in XEmacs.  In both cases,
  1822.      there is no 'top-level catch. (That's why the
  1823.      "bomb-out" hack was added.)
  1824.  
  1825.      #### Fix this horrifitude! 
  1826.      */
  1827.   signal_call_debugger (conditions, sig, data, Qnil, 0,
  1828.             &stack_trace_displayed,
  1829.             &debugger_entered);
  1830.   UNGCPRO;
  1831.   return (throw_or_bomb_out (Qtop_level, Qt, 1, sig, data));
  1832. }
  1833.  
  1834. DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
  1835.   "Signal an error.  Args are SIGNAL-NAME, and associated DATA.\n\
  1836. A signal name is a symbol with an `error-conditions' property\n\
  1837. that is a list of condition names.\n\
  1838. A handler for any of those names will get to handle this signal.\n\
  1839. The symbol `error' should normally be one of them.\n\
  1840. \n\
  1841. DATA should be a list.  Its elements are printed as part of the error message.\n\
  1842. If the signal is handled, DATA is made available to the handler.\n\
  1843. See also the function `condition-case'.")
  1844.   (sig, data)
  1845.      Lisp_Object sig, data;
  1846. {
  1847.   /* Fsignal() is one of these functions that's called all the time
  1848.      with newly-created Lisp objects.  We allow this; but we must GC-
  1849.      protect the objects because all sorts of weird stuff could
  1850.      happen. */
  1851.  
  1852.   struct gcpro gcpro1;
  1853.   GCPRO1 (data);
  1854.   RETURN_UNGCPRO (signal_1 (sig, data));
  1855. }
  1856.  
  1857. /* Utility function.  Doesn't return. */
  1858. DOESNT_RETURN
  1859. signal_error (Lisp_Object sig, Lisp_Object data)
  1860. {
  1861.   for (;;)
  1862.     Fsignal (sig, data);
  1863. }
  1864.  
  1865. DOESNT_RETURN
  1866. signal_simple_error (CONST char *reason, Lisp_Object frob)
  1867. {
  1868.   signal_error (Qerror, list2 (build_translated_string (reason), frob));
  1869. }
  1870.  
  1871. DOESNT_RETURN
  1872. signal_simple_error_2 (CONST char *reason,
  1873.                        Lisp_Object frob0, Lisp_Object frob1)
  1874. {
  1875.   signal_error (Qerror, list3 (build_translated_string (reason), frob0,
  1876.                    frob1));
  1877. }
  1878.  
  1879. Lisp_Object
  1880. signal_simple_continuable_error (CONST char *reason, Lisp_Object frob)
  1881. {
  1882.   return Fsignal (Qerror, list2 (build_translated_string (reason), frob));
  1883. }
  1884.  
  1885. Lisp_Object
  1886. signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
  1887.                    Lisp_Object frob1)
  1888. {
  1889.   return Fsignal (Qerror, list3 (build_translated_string (reason), frob0,
  1890.                  frob1));
  1891. }
  1892.  
  1893. /* This is what the QUIT macro calls to signal a quit */
  1894. void
  1895. signal_quit (void)
  1896. {
  1897.   /* This function can GC */
  1898.   if (EQ (Vquit_flag, Qcritical))
  1899.     debug_on_quit |= 2;        /* set critical bit. */
  1900.   Vquit_flag = Qnil; 
  1901.   Fsignal (Qquit, Qnil);
  1902. }
  1903.  
  1904.  
  1905. /**********************************************************************/
  1906. /*                            User commands                           */
  1907. /**********************************************************************/
  1908.  
  1909. #ifndef standalone
  1910. DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
  1911.   "T if FUNCTION makes provisions for interactive calling.\n\
  1912. This means it contains a description for how to read arguments to give it.\n\
  1913. The value is nil for an invalid function or a symbol with no function\n\
  1914. definition.\n\
  1915. \n\
  1916. Interactively callable functions include strings and vectors (treated\n\
  1917. as keyboard macros), lambda-expressions that contain a top-level call\n\
  1918. to `interactive', autoload definitions made by `autoload' with non-nil\n\
  1919. fourth argument, and some of the built-in functions of Lisp.\n\
  1920. \n\
  1921. Also, a symbol satisfies `commandp' if its function definition does so.")
  1922.   (function)
  1923.      Lisp_Object function;
  1924. {
  1925.   REGISTER Lisp_Object fun;
  1926.   REGISTER Lisp_Object funcar;
  1927.  
  1928.   fun = function;
  1929.  
  1930.   fun = indirect_function (fun, 0);
  1931.   if (EQ (fun, Qunbound))
  1932.     return Qnil;
  1933.  
  1934.   /* Emacs primitives are interactive if their DEFUN specifies an
  1935.      interactive spec.  */
  1936.   if (SUBRP (fun))
  1937.     {
  1938.       if (XSUBR (fun)->prompt)
  1939.     return Qt;
  1940.       else
  1941.     return Qnil;
  1942.     }
  1943.  
  1944.   else if (BYTECODEP (fun))
  1945.     {
  1946.       return (((XBYTECODE (fun)->flags.interactivep) ? Qt : Qnil));
  1947.     }
  1948.  
  1949.   /* Strings and vectors are keyboard macros.  */
  1950.   if (VECTORP (fun) || STRINGP (fun))
  1951.     return Qt;
  1952.  
  1953.   /* Lists may represent commands.  */
  1954.   if (!CONSP (fun))
  1955.     return Qnil;
  1956.   funcar = Fcar (fun);
  1957.   if (!SYMBOLP (funcar))
  1958.     return Fsignal (Qinvalid_function, list1 (fun));
  1959.   if (EQ (funcar, Qlambda))
  1960.     return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
  1961. #ifdef MOCKLISP_SUPPORT
  1962.   if (EQ (funcar, Qmocklisp))
  1963.     return Qt;  /* All mocklisp functions can be called interactively */
  1964. #endif
  1965.   if (EQ (funcar, Qautoload))
  1966.     return Fcar (Fcdr (Fcdr (Fcdr (fun))));
  1967.   else
  1968.     return Qnil;
  1969. }
  1970.  
  1971. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
  1972.  "Execute CMD as an editor command.\n\
  1973. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  1974. Optional second arg RECORD-FLAG is as in 'call-interactively'.")
  1975.      (cmd, record)
  1976.      Lisp_Object cmd, record;
  1977. {
  1978.   /* This function can GC */
  1979.   Lisp_Object prefixarg;
  1980.   Lisp_Object final = cmd;
  1981.   struct backtrace backtrace;
  1982.  
  1983.   prefixarg = Vprefix_arg;
  1984.   Vprefix_arg = Qnil;
  1985.   Vcurrent_prefix_arg = prefixarg;
  1986.  
  1987.   if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil)))
  1988.     {
  1989.     return call1 (Vrun_hooks, Vdisabled_command_hook);
  1990.     }
  1991.  
  1992.   for (;;)
  1993.     {
  1994.       final = indirect_function (cmd, 1);
  1995.       if (CONSP (final) && EQ (Fcar (final), Qautoload))
  1996.     do_autoload (final, cmd);
  1997.       else
  1998.     break;
  1999.     }
  2000.  
  2001.   if (CONSP (final) || SUBRP (final) || BYTECODEP (final))
  2002.     {
  2003. #ifdef EMACS_BTL
  2004.       backtrace.id_number = 0;
  2005. #endif
  2006.       backtrace.next = backtrace_list;
  2007.       backtrace_list = &backtrace;
  2008.       backtrace.function = &Qcall_interactively;
  2009.       backtrace.args = &cmd;
  2010.       backtrace.nargs = 1;
  2011.       backtrace.evalargs = 0;
  2012.       backtrace.pdlcount = specpdl_depth ();
  2013.       backtrace.debug_on_exit = 0;
  2014.  
  2015.       final = Fcall_interactively (cmd, record);
  2016.  
  2017.       backtrace_list = backtrace.next;
  2018.       return (final);
  2019.     }
  2020.   else if (STRINGP (final) || VECTORP (final))
  2021.     {
  2022.       return Fexecute_kbd_macro (final, prefixarg);
  2023.     }
  2024.   else
  2025.     {
  2026.       Fsignal (Qwrong_type_argument,
  2027.            Fcons (Qcommandp,
  2028.               ((EQ (cmd, final))
  2029.                        ? list1 (cmd)
  2030.                        : list2 (cmd, final))));
  2031.       return Qnil;
  2032.     }
  2033. }
  2034.  
  2035. DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
  2036.   "Return t if function in which this appears was called interactively.\n\
  2037. This means that the function was called with call-interactively (which\n\
  2038. includes being called as the binding of a key)\n\
  2039. and input is currently coming from the keyboard (not in keyboard macro).")
  2040.   ()
  2041. {
  2042.   REGISTER struct backtrace *btp;
  2043.   REGISTER Lisp_Object fun;
  2044.  
  2045.   if (!INTERACTIVE)
  2046.     return Qnil;
  2047.  
  2048.   /*  Unless the object was compiled, skip the frame of interactive-p itself
  2049.       (if interpreted) or the frame of byte-code (if called from a compiled
  2050.       function).  Note that *btp->function may be a symbol pointing at a
  2051.       compiled function. */
  2052.   btp = backtrace_list;
  2053.   if (! (BYTECODEP (Findirect_function (*btp->function))))
  2054.     btp = btp->next;
  2055.   for (;
  2056.        btp && (btp->nargs == UNEVALLED
  2057.            || EQ (*btp->function, Qbytecode));
  2058.        btp = btp->next)
  2059.     {}
  2060.   /* btp now points at the frame of the innermost function
  2061.      that DOES eval its args.
  2062.      If it is a built-in function (such as load or eval-region)
  2063.      return nil.  */
  2064.   fun = Findirect_function (*btp->function);
  2065.   /* Beats me why this is necessary, but it is */
  2066.   if (btp && EQ (*btp->function, Qcall_interactively))
  2067.     return Qt;
  2068.   if (SUBRP (fun))
  2069.     return Qnil;
  2070.   /* btp points to the frame of a Lisp function that called interactive-p.
  2071.      Return t if that function was called interactively.  */
  2072.   if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
  2073.     return Qt;
  2074.   return Qnil;
  2075. }
  2076.  
  2077. #endif /* not standalone */
  2078.  
  2079.  
  2080. /**********************************************************************/
  2081. /*                            Autoloading                             */
  2082. /**********************************************************************/
  2083.  
  2084. DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
  2085.   "Define FUNCTION to autoload from FILE.\n\
  2086. FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
  2087. Third arg DOCSTRING is documentation for the function.\n\
  2088. Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
  2089. Fifth arg TYPE indicates the type of the object:\n\
  2090.    nil or omitted says FUNCTION is a function,\n\
  2091.    `keymap' says FUNCTION is really a keymap, and\n\
  2092.    `macro' or t says FUNCTION is really a macro.\n\
  2093. Third through fifth args give info about the real definition.\n\
  2094. They default to nil.\n\
  2095. If FUNCTION is already defined other than as an autoload,\n\
  2096. this does nothing and returns nil.")
  2097.   (function, file, docstring, interactive, type)
  2098.      Lisp_Object function, file, docstring, interactive, type;
  2099. {
  2100.   /* This function can GC */
  2101.   CHECK_SYMBOL (function, 0);
  2102.   CHECK_STRING (file, 1);
  2103.  
  2104.   /* If function is defined and not as an autoload, don't override */
  2105.   if (!EQ (XSYMBOL (function)->function, Qunbound)
  2106.       && !(CONSP (XSYMBOL (function)->function)
  2107.        && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
  2108.     return Qnil;
  2109.  
  2110.   if (purify_flag)
  2111.     {
  2112.       /* Attempt to avoid consing identical (string=) pure strings. */
  2113.       file = Fsymbol_name (Fintern (file, Qnil));
  2114.     }
  2115.  
  2116.   return Ffset (function, 
  2117.                 Fpurecopy (Fcons (Qautoload, list4 (file,
  2118.                                                     docstring,
  2119.                                                     interactive,
  2120.                                                     type))));
  2121. }
  2122.  
  2123. Lisp_Object
  2124. un_autoload (Lisp_Object oldqueue)
  2125. {
  2126.   /* This function can GC */
  2127.   REGISTER Lisp_Object queue, first, second;
  2128.  
  2129.   /* Queue to unwind is current value of Vautoload_queue.
  2130.      oldqueue is the shadowed value to leave in Vautoload_queue.  */
  2131.   queue = Vautoload_queue;
  2132.   Vautoload_queue = oldqueue;
  2133.   while (CONSP (queue))
  2134.     {
  2135.       first = Fcar (queue);
  2136.       second = Fcdr (first);
  2137.       first = Fcar (first);
  2138.       if (EQ (second, Qnil))
  2139.     Vfeatures = first;
  2140.       else
  2141.     Ffset (first, second);
  2142.       queue = Fcdr (queue);
  2143.     }
  2144.   return Qnil;
  2145. }
  2146.  
  2147. void
  2148. do_autoload (Lisp_Object fundef, 
  2149.              Lisp_Object funname)
  2150. {
  2151.   /* This function can GC */
  2152.   int speccount = specpdl_depth_counter;
  2153.   Lisp_Object fun = funname;
  2154.   struct gcpro gcpro1, gcpro2;
  2155.  
  2156.   CHECK_SYMBOL (funname, 1);
  2157.   GCPRO2 (fun, funname);
  2158.  
  2159.   /* Value saved here is to be restored into Vautoload_queue */
  2160.   record_unwind_protect (un_autoload, Vautoload_queue);
  2161.   Vautoload_queue = Qt;
  2162.   call4 (Qload, Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil,
  2163.      Qnil);
  2164.  
  2165.   {
  2166.     Lisp_Object queue = Vautoload_queue;
  2167.  
  2168.     /* Save the old autoloads, in case we ever do an unload. */
  2169.     queue = Vautoload_queue;
  2170.     while (CONSP (queue))
  2171.     {
  2172.       Lisp_Object first = Fcar (queue);
  2173.       Lisp_Object second = Fcdr (first);
  2174.  
  2175.       first = Fcar (first);
  2176.  
  2177.       /* Note: This test is subtle.  The cdr of an autoload-queue entry
  2178.      may be an atom if the autoload entry was generated by a defalias
  2179.      or fset. */
  2180.       if (CONSP (second))
  2181.     Fput (first, Qautoload, (Fcdr (second)));
  2182.  
  2183.       queue = Fcdr (queue);
  2184.     }
  2185.   }
  2186.  
  2187.   /* Once loading finishes, don't undo it.  */
  2188.   Vautoload_queue = Qt;
  2189.   unbind_to (speccount, Qnil);
  2190.  
  2191.   fun = indirect_function (fun, 0);
  2192.  
  2193.   if (EQ (fun, Qunbound)
  2194.       || (CONSP (fun)
  2195.           && EQ (XCAR (fun), Qautoload)))
  2196.     error ("Autoloading failed to define function %s",
  2197.        string_data (XSYMBOL (funname)->name));
  2198.   UNGCPRO;
  2199. }
  2200.  
  2201.  
  2202. /**********************************************************************/
  2203. /*                         eval, funcall, apply                       */
  2204. /**********************************************************************/
  2205.  
  2206. static Lisp_Object funcall_lambda (Lisp_Object fun, 
  2207.                                    int nargs, Lisp_Object args[]);
  2208. static Lisp_Object apply_lambda (Lisp_Object fun, 
  2209.                                  int nargs, Lisp_Object args);
  2210. static Lisp_Object funcall_subr (struct Lisp_Subr *sub, Lisp_Object args[]);
  2211.  
  2212. static int in_warnings;
  2213.  
  2214. static Lisp_Object
  2215. in_warnings_restore (Lisp_Object minimus)
  2216. {
  2217.   in_warnings = 0;
  2218.   return Qnil;
  2219. }
  2220.  
  2221.  
  2222. DEFUN ("eval", Feval, Seval, 1, 1, 0,
  2223.   "Evaluate FORM and return its value.")
  2224.   (form)
  2225.      Lisp_Object form;
  2226. {
  2227.   /* This function can GC */
  2228.   Lisp_Object fun, val, original_fun, original_args;
  2229.   int nargs;
  2230.   struct backtrace backtrace;
  2231.  
  2232.   /* I think this is a pretty safe place to call Lisp code, don't you? */
  2233.   while (!in_warnings && !NILP (Vpending_warnings))
  2234.     {
  2235.       struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  2236.       int speccount = specpdl_depth ();
  2237.       Lisp_Object this_warning_cons, this_warning, class, level, message;
  2238.  
  2239.       record_unwind_protect (in_warnings_restore, Qnil);
  2240.       in_warnings = 1;
  2241.       this_warning_cons = Vpending_warnings;
  2242.       this_warning = XCAR (this_warning_cons);
  2243.       /* in case an error occurs in the warn function, at least
  2244.      it won't happen infinitely */
  2245.       Vpending_warnings = XCDR (Vpending_warnings);
  2246.       free_cons (XCONS (this_warning_cons));
  2247.       class = XCAR (this_warning);
  2248.       level = XCAR (XCDR (this_warning));
  2249.       message = XCAR (XCDR (XCDR (this_warning)));
  2250.       free_list (this_warning);
  2251.       
  2252.       if (NILP (Vpending_warnings))
  2253.     Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary,
  2254.                       but safer */
  2255.  
  2256.       GCPRO4 (form, class, level, message);
  2257.       call3 (Qdisplay_warning, class, message, level);
  2258.       UNGCPRO;
  2259.       unbind_to (speccount, Qnil);
  2260.     }
  2261.  
  2262.   if (!CONSP (form))
  2263.     {
  2264.       if (!SYMBOLP (form))
  2265.     return form;
  2266.  
  2267.       val = Fsymbol_value (form);
  2268.  
  2269. #ifdef MOCKLISP_SUPPORT
  2270.       if (!EQ (Vmocklisp_arguments, Qt))
  2271.     {
  2272.       if (NILP (val))
  2273.         val = Qzero;
  2274.       else if (EQ (val, Qt))
  2275.         val = make_number (1);
  2276.     }
  2277. #endif
  2278.       return val;
  2279.     }
  2280.  
  2281.   QUIT;
  2282.   if ((consing_since_gc > gc_cons_threshold) || always_gc)
  2283.     {
  2284.       struct gcpro gcpro1;
  2285.       GCPRO1 (form);
  2286.       garbage_collect_1 ();
  2287.       UNGCPRO;
  2288.     }
  2289.  
  2290.   if (++lisp_eval_depth > max_lisp_eval_depth)
  2291.     {
  2292.       if (max_lisp_eval_depth < 100)
  2293.     max_lisp_eval_depth = 100;
  2294.       if (lisp_eval_depth > max_lisp_eval_depth)
  2295.     error ("Lisp nesting exceeds `max-lisp-eval-depth'");
  2296.     }
  2297.  
  2298.   original_fun = Fcar (form);
  2299.   original_args = Fcdr (form);
  2300.   nargs = XINT (Flength (original_args));
  2301.  
  2302. #ifdef EMACS_BTL
  2303.   backtrace.id_number = 0;
  2304. #endif
  2305.   backtrace.pdlcount = specpdl_depth_counter;
  2306.   backtrace.next = backtrace_list;
  2307.   backtrace_list = &backtrace;
  2308.   backtrace.function = &original_fun; /* This also protects them from gc */
  2309.   backtrace.args = &original_args;
  2310.   backtrace.nargs = UNEVALLED;
  2311.   backtrace.evalargs = 1;
  2312.   backtrace.debug_on_exit = 0;
  2313.  
  2314.   if (debug_on_next_call)
  2315.     do_debug_on_call (Qt);
  2316.  
  2317.   /* At this point, only original_fun and original_args
  2318.      have values that will be used below */
  2319.  retry:
  2320.   fun = indirect_function (original_fun, 1);
  2321.  
  2322.   if (SUBRP (fun))
  2323.     {
  2324.       struct Lisp_Subr *subr = XSUBR (fun);
  2325.       int max_args = subr->max_args;
  2326.       Lisp_Object argvals[SUBR_MAX_ARGS];
  2327.       Lisp_Object args_left;
  2328.       REGISTER int i;
  2329.  
  2330.       args_left = original_args;
  2331.  
  2332.       if (nargs < subr->min_args
  2333.       || (max_args >= 0 && max_args < nargs))
  2334.     {
  2335.       return Fsignal (Qwrong_number_of_arguments, 
  2336.               list2 (fun, make_number (nargs)));
  2337.     }
  2338.  
  2339.       if (max_args == UNEVALLED)
  2340.     {
  2341.       backtrace.evalargs = 0;
  2342.       val = ((subr_function (subr)) (args_left));
  2343.     }
  2344.  
  2345.       else if (max_args == MANY)
  2346.     {
  2347.       /* Pass a vector of evaluated arguments */
  2348.       Lisp_Object *vals;
  2349.       REGISTER int argnum;
  2350.           struct gcpro gcpro1, gcpro2, gcpro3;
  2351.  
  2352.       vals = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object));
  2353.  
  2354.       GCPRO3 (args_left, fun, vals[0]);
  2355.       gcpro3.nvars = 0;
  2356.  
  2357.       argnum = 0;
  2358.           while (!NILP (args_left))
  2359.         {
  2360.           vals[argnum++] = Feval (Fcar (args_left));
  2361.           args_left = Fcdr (args_left);
  2362.           gcpro3.nvars = argnum;
  2363.         }
  2364.  
  2365.       backtrace.args = vals;
  2366.       backtrace.nargs = nargs;
  2367.  
  2368.       val = ((subr_function (subr)) (nargs, vals));
  2369.  
  2370.           /* Have to duplicate this code because if the
  2371.            *  debugger is called it must be in a scope in
  2372.            *  which the `alloca'-ed data in vals is still valid.
  2373.            *  (And GC-protected.)
  2374.            */
  2375.           lisp_eval_depth--;
  2376. #ifdef MOCKLISP_SUPPORT
  2377.           if (!EQ (Vmocklisp_arguments, Qt))
  2378.         {
  2379.           if (NILP (val))
  2380.         val = Qzero;
  2381.           else if (EQ (val, Qt))
  2382.         val = make_number (1);
  2383.         }
  2384. #endif
  2385.           if (backtrace.debug_on_exit)
  2386.             val = do_debug_on_exit (val);
  2387.           backtrace_list = backtrace.next;
  2388.       UNGCPRO;
  2389.           return (val);
  2390.     }
  2391.  
  2392.       else
  2393.         {
  2394.           struct gcpro gcpro1, gcpro2, gcpro3;
  2395.  
  2396.       GCPRO3 (args_left, fun, fun);
  2397.       gcpro3.var = argvals;
  2398.       gcpro3.nvars = 0;
  2399.  
  2400.       for (i = 0; i < nargs; args_left = Fcdr (args_left))
  2401.         {
  2402.           argvals[i] = Feval (Fcar (args_left));
  2403.           gcpro3.nvars = ++i;
  2404.         }
  2405.       
  2406.       UNGCPRO;
  2407.       
  2408.       for (i = nargs; i < max_args; i++)
  2409.             argvals[i] = Qnil;
  2410.  
  2411.           backtrace.args = argvals;
  2412.           backtrace.nargs = nargs;
  2413.  
  2414.           val = funcall_subr (subr, argvals);
  2415.         }
  2416.     }
  2417.   else if (BYTECODEP (fun))
  2418.     val = apply_lambda (fun, nargs, original_args);
  2419.   else
  2420.     {
  2421.       Lisp_Object funcar;
  2422.  
  2423.       if (!CONSP (fun))
  2424.         goto invalid_function;
  2425.       funcar = Fcar (fun);
  2426.       if (!SYMBOLP (funcar))
  2427.         goto invalid_function;
  2428.       if (EQ (funcar, Qautoload))
  2429.     {
  2430.       do_autoload (fun, original_fun);
  2431.       goto retry;
  2432.     }
  2433.       if (EQ (funcar, Qmacro))
  2434.     val = Feval (apply1 (Fcdr (fun), original_args));
  2435.       else if (EQ (funcar, Qlambda))
  2436.         val = apply_lambda (fun, nargs, original_args);
  2437. #ifdef MOCKLISP_SUPPORT
  2438.       else if (EQ (funcar, Qmocklisp))
  2439.     val = ml_apply (fun, original_args);
  2440. #endif
  2441.       else
  2442.     {
  2443.     invalid_function:
  2444.       return Fsignal (Qinvalid_function, list1 (fun));
  2445.     }
  2446.     }
  2447.  
  2448.   lisp_eval_depth--;
  2449. #ifdef MOCKLISP_SUPPORT
  2450.   if (!EQ (Vmocklisp_arguments, Qt))
  2451.     {
  2452.       if (NILP (val))
  2453.     val = Qzero;
  2454.       else if (EQ (val, Qt))
  2455.     val = make_number (1);
  2456.     }
  2457. #endif
  2458.   if (backtrace.debug_on_exit)
  2459.     val = do_debug_on_exit (val);
  2460.   backtrace_list = backtrace.next;
  2461.   return (val);
  2462. }
  2463.  
  2464.  
  2465. DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
  2466.   "Call first argument as a function, passing remaining arguments to it.\n\
  2467. Thus, (funcall 'cons 'x 'y) returns (x . y).")
  2468.   (nargs, args)
  2469.      int nargs;
  2470.      Lisp_Object *args;
  2471. {
  2472.   /* This function can GC */
  2473.   Lisp_Object fun;
  2474.   Lisp_Object val;
  2475.   struct backtrace backtrace;
  2476.   REGISTER int i;
  2477.  
  2478.   QUIT;
  2479.   if ((consing_since_gc > gc_cons_threshold) || always_gc)
  2480.     /* Callers should gcpro lexpr args */
  2481.     garbage_collect_1 ();
  2482.  
  2483.   if (++lisp_eval_depth > max_lisp_eval_depth)
  2484.     {
  2485.       if (max_lisp_eval_depth < 100)
  2486.     max_lisp_eval_depth = 100;
  2487.       if (lisp_eval_depth > max_lisp_eval_depth)
  2488.     error ("Lisp nesting exceeds `max-lisp-eval-depth'");
  2489.     }
  2490.  
  2491.   /* Count number of arguments to function */
  2492.   nargs = nargs - 1;
  2493.  
  2494. #ifdef EMACS_BTL
  2495.   backtrace.id_number = 0;
  2496. #endif
  2497.   backtrace.pdlcount = specpdl_depth_counter;
  2498.   backtrace.next = backtrace_list;
  2499.   backtrace_list = &backtrace;
  2500.   backtrace.function = &args[0];
  2501.   backtrace.args = &args[1];
  2502.   backtrace.nargs = nargs;
  2503.   backtrace.evalargs = 0;
  2504.   backtrace.debug_on_exit = 0;
  2505.  
  2506.   if (debug_on_next_call)
  2507.     do_debug_on_call (Qlambda);
  2508.  
  2509.  retry:
  2510.  
  2511.   fun = args[0];
  2512.  
  2513. #ifdef EMACS_BTL
  2514.   {
  2515.     extern int emacs_btl_elisp_only_p;
  2516.     extern int btl_symbol_id_number ();
  2517.     if (emacs_btl_elisp_only_p)
  2518.       backtrace.id_number = btl_symbol_id_number (fun);
  2519.   }
  2520. #endif
  2521.  
  2522.   if (SYMBOLP (fun))
  2523.     fun = indirect_function (fun, 1);
  2524.  
  2525.   if (SUBRP (fun))
  2526.     {
  2527.       struct Lisp_Subr *subr = XSUBR (fun);
  2528.       int max_args = subr->max_args;
  2529.  
  2530.       if (max_args == UNEVALLED)
  2531.     return Fsignal (Qinvalid_function, list1 (fun));
  2532.  
  2533.       if (nargs < subr->min_args
  2534.       || (max_args >= 0 && max_args < nargs))
  2535.     {
  2536.       return Fsignal (Qwrong_number_of_arguments, 
  2537.                           list2 (fun, make_number (nargs)));
  2538.     }
  2539.  
  2540.       if (max_args == MANY)
  2541.     {
  2542.       val = ((subr_function (subr)) (nargs, args + 1));
  2543.     }
  2544.  
  2545.       else if (max_args > nargs)
  2546.     {
  2547.           Lisp_Object argvals[SUBR_MAX_ARGS];
  2548.  
  2549.           /* Default optionals to nil */
  2550.           for (i = 0; i < nargs; i++)
  2551.             argvals[i] = args[i + 1];
  2552.       for (i = nargs; i < max_args; i++)
  2553.         argvals[i] = Qnil;
  2554.  
  2555.           val = funcall_subr (subr, argvals);
  2556.     }
  2557.       else
  2558.         val = funcall_subr (subr, args + 1);
  2559.     }
  2560.   else if (BYTECODEP (fun))
  2561.     val = funcall_lambda (fun, nargs, args + 1);
  2562.   else if (!CONSP (fun))
  2563.     {
  2564.     invalid_function:
  2565.       return Fsignal (Qinvalid_function, list1 (fun));
  2566.     }
  2567.   else
  2568.     {
  2569.       Lisp_Object funcar = Fcar (fun);
  2570.  
  2571.       if (!SYMBOLP (funcar))
  2572.         goto invalid_function;
  2573.       if (EQ (funcar, Qlambda))
  2574.     val = funcall_lambda (fun, nargs, args + 1);
  2575. #ifdef MOCKLISP_SUPPORT
  2576.       else if (EQ (funcar, Qmocklisp))
  2577.     val = ml_apply (fun, Flist (nargs, args + 1));
  2578. #endif
  2579.       else if (EQ (funcar, Qautoload))
  2580.     {
  2581.       do_autoload (fun, args[0]);
  2582.       goto retry;
  2583.     }
  2584.       else
  2585.     {
  2586.           goto invalid_function;
  2587.     }
  2588.     }
  2589.   lisp_eval_depth--;
  2590.   if (backtrace.debug_on_exit)
  2591.     val = do_debug_on_exit (val);
  2592.   backtrace_list = backtrace.next;
  2593.   return val;
  2594. }
  2595.  
  2596.  
  2597. DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
  2598.   "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
  2599. Thus, (apply '+ 1 2 '(3 4)) returns 10.")
  2600.   (nargs, args)
  2601.      int nargs;
  2602.      Lisp_Object *args;
  2603. {
  2604.   /* This function can GC */
  2605.   Lisp_Object fun = args[0];
  2606.   Lisp_Object spread_arg = args [nargs - 1];
  2607.   int numargs;
  2608.   int funcall_nargs;
  2609.  
  2610.   CHECK_LIST (spread_arg, nargs);
  2611.   
  2612.   numargs = XINT (Flength (spread_arg));
  2613.  
  2614.   if (numargs == 0)
  2615.     /* (apply foo 0 1 '()) */
  2616.     return Ffuncall (nargs - 1, args);
  2617.   else if (numargs == 1)
  2618.     {
  2619.       /* (apply foo 0 1 '(2)) */
  2620.       args [nargs - 1] = XCAR (spread_arg);
  2621.       return Ffuncall (nargs, args);
  2622.     }
  2623.  
  2624.   /* -1 for function, -1 for spread arg */
  2625.   numargs = nargs - 2 + numargs;
  2626.   /* +1 for function */
  2627.   funcall_nargs = 1 + numargs;
  2628.  
  2629.   if (SYMBOLP (fun))
  2630.     fun = indirect_function (fun, 0);
  2631.   if (EQ (fun, Qunbound))
  2632.     {
  2633.       /* Let funcall get the error */
  2634.       fun = args[0];
  2635.     }
  2636.   else if (SUBRP (fun))
  2637.     {
  2638.       struct Lisp_Subr *subr = XSUBR (fun);
  2639.       int max_args = subr->max_args;
  2640.  
  2641.       if (numargs < subr->min_args
  2642.       || (max_args >= 0 && max_args < numargs))
  2643.         {
  2644.           /* Let funcall get the error */
  2645.         }
  2646.       else if (max_args > numargs)
  2647.     {
  2648.       /* Avoid having funcall cons up yet another new vector of arguments
  2649.          by explicitly supplying nil's for optional values */
  2650.           funcall_nargs += (max_args - numargs);
  2651.         }
  2652.     }
  2653.   {
  2654.     REGISTER int i;
  2655.     REGISTER Lisp_Object *funcall_args
  2656.       = (Lisp_Object *) alloca (funcall_nargs * sizeof (Lisp_Object));
  2657.     struct gcpro gcpro1;
  2658.  
  2659.     GCPRO1 (*funcall_args);
  2660.     gcpro1.nvars = funcall_nargs;
  2661.  
  2662.     /* Copy in the unspread args */
  2663.     memcpy (funcall_args, args, (nargs - 1) * sizeof (Lisp_Object));
  2664.     /* Spread the last arg we got.  Its first element goes in
  2665.        the slot that it used to occupy, hence this value of I.  */
  2666.     for (i = nargs - 1; 
  2667.          !NILP (spread_arg);    /* i < 1 + numargs */
  2668.          i++, spread_arg = XCDR (spread_arg))
  2669.       {
  2670.     funcall_args [i] = XCAR (spread_arg);
  2671.       }
  2672.     /* Supply nil for optional args (to subrs) */
  2673.     for (; i < funcall_nargs; i++)
  2674.       funcall_args[i] = Qnil;
  2675.  
  2676.  
  2677.     RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
  2678.   }
  2679. }
  2680.  
  2681.  
  2682. static Lisp_Object
  2683. funcall_subr (struct Lisp_Subr *subr, Lisp_Object args[])
  2684. {
  2685.   Lisp_Object (*fn) () = subr_function (subr);
  2686.   switch (subr->max_args)
  2687.     {
  2688.     case 0:
  2689.       return ((*fn) ());
  2690.     case 1:
  2691.       return ((*fn) (args[0]));
  2692.     case 2:
  2693.       return ((*fn) (args[0], args[1]));
  2694.     case 3:
  2695.       return ((*fn) (args[0], args[1], args[2]));
  2696.     case 4:
  2697.       return ((*fn) (args[0], args[1], args[2], args[3]));
  2698.     case 5:
  2699.       return ((*fn) (args[0], args[1], args[2], args[3], args[4]));
  2700.     case 6:
  2701.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5]));
  2702.     case 7:
  2703.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2704.              args[6]));
  2705.     case 8:
  2706.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2707.              args[6], args[7]));
  2708.     case 9:
  2709.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2710.              args[6], args[7], args[8]));
  2711.     case 10:
  2712.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2713.              args[6], args[7], args[8], args[9]));
  2714.     case 11:
  2715.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2716.              args[6], args[7], args[8], args[9], args[10]));
  2717.     case 12:
  2718.       return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
  2719.              args[6], args[7], args[8], args[9], args[10], args[11]));
  2720.     default:
  2721.       /* Someone has created a subr that takes more arguments than
  2722.      is supported by this code.  We need to either rewrite the
  2723.      subr to use a different argument protocol, or add more
  2724.      cases to this switch.  */
  2725.       abort ();
  2726.     }
  2727.   return Qnil;    /* suppress compiler warning */
  2728. }
  2729.  
  2730. static Lisp_Object
  2731. apply_lambda (Lisp_Object fun, int numargs, Lisp_Object unevalled_args)
  2732. {
  2733.   /* This function can GC */
  2734.   struct gcpro gcpro1, gcpro2, gcpro3;
  2735.   REGISTER int i;
  2736.   REGISTER Lisp_Object tem;
  2737.   REGISTER Lisp_Object *arg_vector
  2738.     = (Lisp_Object *) alloca (numargs * sizeof (Lisp_Object));
  2739.  
  2740.   GCPRO3 (*arg_vector, unevalled_args, fun);
  2741.   gcpro1.nvars = 0;
  2742.  
  2743.   for (i = 0; i < numargs;)
  2744.     {
  2745.       tem = Fcar (unevalled_args), unevalled_args = Fcdr (unevalled_args);
  2746.       tem = Feval (tem);
  2747.       arg_vector[i++] = tem;
  2748.       gcpro1.nvars = i;
  2749.     }
  2750.  
  2751.   UNGCPRO;
  2752.  
  2753.   backtrace_list->args = arg_vector;
  2754.   backtrace_list->nargs = i;
  2755.   backtrace_list->evalargs = 0;
  2756.   tem = funcall_lambda (fun, numargs, arg_vector);
  2757.  
  2758.   /* Do the debug-on-exit now, while arg_vector still exists.  */
  2759.   if (backtrace_list->debug_on_exit)
  2760.     tem = do_debug_on_exit (tem);
  2761.   /* Don't do it again when we return to eval.  */
  2762.   backtrace_list->debug_on_exit = 0;
  2763.   return (tem);
  2764. }
  2765.  
  2766. /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
  2767.    and return the result of evaluation.
  2768.    FUN must be either a lambda-expression or a compiled-code object.  */
  2769.  
  2770. static Lisp_Object
  2771. funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object arg_vector[])
  2772. {
  2773.   /* This function can GC */
  2774.   Lisp_Object val, tem;
  2775.   REGISTER Lisp_Object syms_left;
  2776.   REGISTER Lisp_Object next;
  2777.   int speccount = specpdl_depth_counter;
  2778.   REGISTER int i;
  2779.   int optional = 0, rest = 0;
  2780.  
  2781. #ifdef MOCKLISP_SUPPORT
  2782.   if (!EQ (Vmocklisp_arguments, Qt))
  2783.     specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
  2784. #endif
  2785.  
  2786.   if (CONSP (fun))
  2787.     syms_left = Fcar (Fcdr (fun));
  2788.   else if (BYTECODEP (fun))
  2789.     syms_left = XBYTECODE (fun)->arglist;
  2790.   else abort ();
  2791.  
  2792.   i = 0;
  2793.   for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
  2794.     {
  2795.       QUIT;
  2796.       next = Fcar (syms_left);
  2797.       if (!SYMBOLP (next))
  2798.     signal_error (Qinvalid_function, list1 (fun));
  2799.       if (EQ (next, Qand_rest))
  2800.     rest = 1;
  2801.       else if (EQ (next, Qand_optional))
  2802.     optional = 1;
  2803.       else if (rest)
  2804.     {
  2805.       specbind (next, Flist (nargs - i, &arg_vector[i]));
  2806.       i = nargs;
  2807.     }
  2808.       else if (i < nargs)
  2809.     {
  2810.       tem = arg_vector[i++];
  2811.       specbind (next, tem);
  2812.     }
  2813.       else if (!optional)
  2814.     return Fsignal (Qwrong_number_of_arguments, 
  2815.                         list2 (fun, make_number (nargs)));
  2816.       else
  2817.     specbind (next, Qnil);
  2818.     }
  2819.  
  2820.   if (i < nargs)
  2821.     return Fsignal (Qwrong_number_of_arguments, 
  2822.                     list2 (fun, make_number (nargs)));
  2823.  
  2824.   if (CONSP (fun))
  2825.     val = Fprogn (Fcdr (Fcdr (fun)));
  2826.   else
  2827.     {
  2828.       struct Lisp_Bytecode *b = XBYTECODE (fun);
  2829.       val = Fbyte_code (b->bytecodes,
  2830.                         b->constants,
  2831.                         make_number (b->maxdepth));
  2832.     }
  2833.   return unbind_to (speccount, val);
  2834. }
  2835.  
  2836.  
  2837. /**********************************************************************/
  2838. /*                  Front-ends to eval, funcall, apply                */
  2839. /**********************************************************************/
  2840.  
  2841. /* Apply fn to arg */
  2842. Lisp_Object
  2843. apply1 (Lisp_Object fn, Lisp_Object arg)
  2844. {
  2845.   /* This function can GC */
  2846.   struct gcpro gcpro1;
  2847.   Lisp_Object args[2];
  2848.  
  2849.   if (NILP (arg))
  2850.     return (Ffuncall (1, &fn));
  2851.   GCPRO1 (args[0]);
  2852.   gcpro1.nvars = 2;
  2853.   args[0] = fn;
  2854.   args[1] = arg;
  2855.   RETURN_UNGCPRO (Fapply (2, args));
  2856. }
  2857.  
  2858. /* Call function fn on no arguments */
  2859. Lisp_Object
  2860. call0 (Lisp_Object fn)
  2861. {
  2862.   /* This function can GC */
  2863.   struct gcpro gcpro1;
  2864.  
  2865.   GCPRO1 (fn);
  2866.   RETURN_UNGCPRO (Ffuncall (1, &fn));
  2867. }
  2868.  
  2869. /* Call function fn with argument arg0 */
  2870. Lisp_Object
  2871. call1 (Lisp_Object fn,
  2872.        Lisp_Object arg0)
  2873. {
  2874.   /* This function can GC */
  2875.   struct gcpro gcpro1;
  2876.   Lisp_Object args[2];  
  2877.   args[0] = fn;
  2878.   args[1] = arg0;
  2879.   GCPRO1 (args[0]);
  2880.   gcpro1.nvars = 2;
  2881.   RETURN_UNGCPRO (Ffuncall (2, args));
  2882. }
  2883.  
  2884. /* Call function fn with arguments arg0, arg1 */
  2885. Lisp_Object
  2886. call2 (Lisp_Object fn,
  2887.        Lisp_Object arg0, Lisp_Object arg1)
  2888. {
  2889.   /* This function can GC */
  2890.   struct gcpro gcpro1;
  2891.   Lisp_Object args[3];
  2892.   args[0] = fn;
  2893.   args[1] = arg0;
  2894.   args[2] = arg1;
  2895.   GCPRO1 (args[0]);
  2896.   gcpro1.nvars = 3;
  2897.   RETURN_UNGCPRO (Ffuncall (3, args));
  2898. }
  2899.  
  2900. /* Call function fn with arguments arg0, arg1, arg2 */
  2901. Lisp_Object
  2902. call3 (Lisp_Object fn,
  2903.        Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
  2904. {
  2905.   /* This function can GC */
  2906.   struct gcpro gcpro1;
  2907.   Lisp_Object args[4];
  2908.   args[0] = fn;
  2909.   args[1] = arg0;
  2910.   args[2] = arg1;
  2911.   args[3] = arg2;
  2912.   GCPRO1 (args[0]);
  2913.   gcpro1.nvars = 4;
  2914.   RETURN_UNGCPRO (Ffuncall (4, args));
  2915. }
  2916.  
  2917. /* Call function fn with arguments arg0, arg1, arg2, arg3 */
  2918. Lisp_Object
  2919. call4 (Lisp_Object fn,
  2920.        Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
  2921.        Lisp_Object arg3)
  2922. {
  2923.   /* This function can GC */
  2924.   struct gcpro gcpro1;
  2925.   Lisp_Object args[5];
  2926.   args[0] = fn;
  2927.   args[1] = arg0;
  2928.   args[2] = arg1;
  2929.   args[3] = arg2;
  2930.   args[4] = arg3;
  2931.   GCPRO1 (args[0]);
  2932.   gcpro1.nvars = 5;
  2933.   RETURN_UNGCPRO (Ffuncall (5, args));
  2934. }
  2935.  
  2936. /* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */
  2937. Lisp_Object
  2938. call5 (Lisp_Object fn,
  2939.        Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, 
  2940.        Lisp_Object arg3, Lisp_Object arg4)
  2941. {
  2942.   /* This function can GC */
  2943.   struct gcpro gcpro1;
  2944.   Lisp_Object args[6];
  2945.   args[0] = fn;
  2946.   args[1] = arg0;
  2947.   args[2] = arg1;
  2948.   args[3] = arg2;
  2949.   args[4] = arg3;
  2950.   args[5] = arg4;
  2951.   GCPRO1 (args[0]);
  2952.   gcpro1.nvars = 6;
  2953.   RETURN_UNGCPRO (Ffuncall (6, args));
  2954. }
  2955.  
  2956. Lisp_Object
  2957. call6 (Lisp_Object fn,
  2958.        Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, 
  2959.        Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
  2960. {
  2961.   /* This function can GC */
  2962.   struct gcpro gcpro1;
  2963.   Lisp_Object args[7];
  2964.   args[0] = fn;
  2965.   args[1] = arg0;
  2966.   args[2] = arg1;
  2967.   args[3] = arg2;
  2968.   args[4] = arg3;
  2969.   args[5] = arg4;
  2970.   args[6] = arg5;
  2971.   GCPRO1 (args[0]);
  2972.   gcpro1.nvars = 7;
  2973.   RETURN_UNGCPRO (Ffuncall (7, args));
  2974. }
  2975.  
  2976. Lisp_Object
  2977. call0_in_buffer (struct buffer *buf, Lisp_Object fn)
  2978. {
  2979.   int speccount = specpdl_depth ();
  2980.   Lisp_Object val;
  2981.  
  2982.   if (current_buffer != buf)
  2983.     {
  2984.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  2985.       set_buffer_internal (buf);
  2986.     }
  2987.   val = call0 (fn);
  2988.   unbind_to (speccount, Qnil);
  2989.   return val;
  2990. }
  2991.  
  2992. Lisp_Object
  2993. call1_in_buffer (struct buffer *buf, Lisp_Object fn,
  2994.          Lisp_Object arg0)
  2995. {
  2996.   int speccount = specpdl_depth ();
  2997.   Lisp_Object val;
  2998.  
  2999.   if (current_buffer != buf)
  3000.     {
  3001.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3002.       set_buffer_internal (buf);
  3003.     }
  3004.   val = call1 (fn, arg0);
  3005.   unbind_to (speccount, Qnil);
  3006.   return val;
  3007. }
  3008.  
  3009. Lisp_Object
  3010. call2_in_buffer (struct buffer *buf, Lisp_Object fn,
  3011.          Lisp_Object arg0, Lisp_Object arg1)
  3012. {
  3013.   int speccount = specpdl_depth ();
  3014.   Lisp_Object val;
  3015.  
  3016.   if (current_buffer != buf)
  3017.     {
  3018.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3019.       set_buffer_internal (buf);
  3020.     }
  3021.   val = call2 (fn, arg0, arg1);
  3022.   unbind_to (speccount, Qnil);
  3023.   return val;
  3024. }
  3025.  
  3026. Lisp_Object
  3027. call3_in_buffer (struct buffer *buf, Lisp_Object fn,
  3028.          Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
  3029. {
  3030.   int speccount = specpdl_depth ();
  3031.   Lisp_Object val;
  3032.  
  3033.   if (current_buffer != buf)
  3034.     {
  3035.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3036.       set_buffer_internal (buf);
  3037.     }
  3038.   val = call3 (fn, arg0, arg1, arg2);
  3039.   unbind_to (speccount, Qnil);
  3040.   return val;
  3041. }
  3042.  
  3043. Lisp_Object
  3044. call4_in_buffer (struct buffer *buf, Lisp_Object fn,
  3045.          Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
  3046.          Lisp_Object arg3)
  3047. {
  3048.   int speccount = specpdl_depth ();
  3049.   Lisp_Object val;
  3050.  
  3051.   if (current_buffer != buf)
  3052.     {
  3053.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3054.       set_buffer_internal (buf);
  3055.     }
  3056.   val = call4 (fn, arg0, arg1, arg2, arg3);
  3057.   unbind_to (speccount, Qnil);
  3058.   return val;
  3059. }
  3060.  
  3061. Lisp_Object
  3062. call5_in_buffer (struct buffer *buf, Lisp_Object fn,
  3063.          Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
  3064.          Lisp_Object arg3, Lisp_Object arg4)
  3065. {
  3066.   int speccount = specpdl_depth ();
  3067.   Lisp_Object val;
  3068.  
  3069.   if (current_buffer != buf)
  3070.     {
  3071.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3072.       set_buffer_internal (buf);
  3073.     }
  3074.   val = call5 (fn, arg0, arg1, arg2, arg3, arg4);
  3075.   unbind_to (speccount, Qnil);
  3076.   return val;
  3077. }
  3078.  
  3079. Lisp_Object
  3080. call6_in_buffer (struct buffer *buf, Lisp_Object fn,
  3081.          Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
  3082.          Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
  3083. {
  3084.   int speccount = specpdl_depth ();
  3085.   Lisp_Object val;
  3086.  
  3087.   if (current_buffer != buf)
  3088.     {
  3089.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3090.       set_buffer_internal (buf);
  3091.     }
  3092.   val = call6 (fn, arg0, arg1, arg2, arg3, arg4, arg5);
  3093.   unbind_to (speccount, Qnil);
  3094.   return val;
  3095. }
  3096.  
  3097. Lisp_Object
  3098. eval_in_buffer (struct buffer *buf, Lisp_Object form)
  3099. {
  3100.   int speccount = specpdl_depth ();
  3101.   Lisp_Object val;
  3102.  
  3103.   if (current_buffer != buf)
  3104.     {
  3105.       record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  3106.       set_buffer_internal (buf);
  3107.     }
  3108.   val = Feval (form);
  3109.   unbind_to (speccount, Qnil);
  3110.   return val;
  3111. }
  3112.  
  3113.  
  3114. /***** Error-catching front-ends to eval, funcall, apply */
  3115.  
  3116. /* Call function fn on no arguments, with condition handler */
  3117. Lisp_Object
  3118. call0_with_handler (Lisp_Object handler, Lisp_Object fn)
  3119. {
  3120.   /* This function can GC */
  3121.   struct gcpro gcpro1;
  3122.   Lisp_Object args[2];
  3123.   args[0] = handler;
  3124.   args[1] = fn;
  3125.   GCPRO1 (args[0]);
  3126.   gcpro1.nvars = 2;
  3127.   RETURN_UNGCPRO (Fcall_with_condition_handler (2, args));
  3128. }
  3129.  
  3130. /* Call function fn with argument arg0, with condition handler */
  3131. Lisp_Object
  3132. call1_with_handler (Lisp_Object handler, Lisp_Object fn,
  3133.                     Lisp_Object arg0)
  3134. {
  3135.   /* This function can GC */
  3136.   struct gcpro gcpro1;
  3137.   Lisp_Object args[3];
  3138.   args[0] = handler;
  3139.   args[1] = fn;
  3140.   args[2] = arg0;
  3141.   GCPRO1 (args[0]);
  3142.   gcpro1.nvars = 3;
  3143.   RETURN_UNGCPRO (Fcall_with_condition_handler (3, args));
  3144. }
  3145.  
  3146.  
  3147. /* The following functions provide you with error-trapping versions
  3148.    of the various front-ends above.  They take an additional
  3149.    "warning_string" argument; if non-zero, a warning with this
  3150.    string and the actual error that occurred will be displayed
  3151.    in the *Warnings* buffer if an error occurs.  In all cases,
  3152.    QUIT is inhibited while these functions are running, and if
  3153.    an error occurs, Qunbound is returned instead of the normal
  3154.    return value.
  3155.    */
  3156.  
  3157. /* #### This stuff needs to catch throws as well.  We need to 
  3158.    improve internal_catch() so it can take a "catch anything"
  3159.    argument similar to Qt or Qerror for condition_case_1(). */
  3160.  
  3161. static Lisp_Object
  3162. caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
  3163. {
  3164.   if (!NILP (errordata))
  3165.     {
  3166.       char *str = (char *) get_opaque_ptr (arg);
  3167.       Lisp_Object args[2];
  3168.       
  3169.       args[0] = build_string (str);
  3170.       /* #### This should call
  3171.      (with-output-to-string (display-error errordata))
  3172.      but that stuff is all in Lisp currently. */
  3173.       args[1] = errordata;
  3174.       warn_when_safe_lispstr
  3175.     (Qerror, Qwarning, emacs_doprnt_string_lisp ((Bufbyte *) "%s: %s",
  3176.                              Qnil, -1, 2, args));
  3177.     }
  3178.   return Qunbound;
  3179. }
  3180.  
  3181. static Lisp_Object
  3182. catch_them_squirmers_eval_in_buffer (Lisp_Object cons)
  3183. {
  3184.   return eval_in_buffer (XBUFFER (XCAR (cons)), XCDR (cons));
  3185. }
  3186.  
  3187. Lisp_Object
  3188. eval_in_buffer_trapping_errors (char *warning_string,
  3189.                 struct buffer *buf, Lisp_Object form)
  3190. {
  3191.   int speccount = specpdl_depth ();
  3192.   Lisp_Object tem;
  3193.   Lisp_Object buffer = Qnil;
  3194.   Lisp_Object cons;
  3195.  
  3196.   XSETBUFFER (buffer, buf);
  3197.  
  3198.   specbind (Qinhibit_quit, Qt);
  3199.   /* gc_currently_forbidden = 1; Currently no reason to do this; */
  3200.  
  3201.   /* Qerror not Qt, so you can get a backtrace */
  3202.   cons = Fcons (buffer, form);
  3203.   tem = condition_case_1 (Qerror,
  3204.                           catch_them_squirmers_eval_in_buffer,
  3205.               cons,
  3206.                           caught_a_squirmer,
  3207.               warning_string ? make_opaque_ptr (warning_string) :
  3208.               Qnil);
  3209.  
  3210.   /* gc_currently_forbidden = 0; */
  3211.   return unbind_to (speccount, tem);
  3212. }
  3213.  
  3214. static Lisp_Object
  3215. catch_them_squirmers_run_hook (Lisp_Object hook_symbol)
  3216. {
  3217.   /* This function can GC */
  3218.   return call1 (Vrun_hooks, hook_symbol);
  3219. }
  3220.  
  3221. Lisp_Object
  3222. run_hook_trapping_errors (char *warning_string, Lisp_Object hook_symbol)
  3223. {
  3224.   int speccount = specpdl_depth ();
  3225.   Lisp_Object tem;
  3226.  
  3227.   if (NILP (Vrun_hooks))
  3228.     return (Qnil);
  3229.   tem = find_symbol_value (hook_symbol);
  3230.   if (NILP (tem) || EQ (tem, Qunbound))
  3231.     return (Qnil);
  3232.  
  3233.   specbind (Qinhibit_quit, Qt);
  3234.  
  3235.   /* Qerror not Qt, so you can get a backtrace */
  3236.   tem = condition_case_1 (Qerror,
  3237.                           catch_them_squirmers_run_hook,
  3238.               hook_symbol,
  3239.                           caught_a_squirmer,
  3240.               warning_string ? make_opaque_ptr (warning_string) :
  3241.               Qnil);
  3242.  
  3243.   return unbind_to (speccount, tem);
  3244. }
  3245.  
  3246. static Lisp_Object
  3247. catch_them_squirmers_call0 (Lisp_Object function)
  3248. {
  3249.   /* This function can GC */
  3250.   return call0 (function);
  3251. }
  3252.  
  3253. Lisp_Object
  3254. call0_trapping_errors (char *warning_string, Lisp_Object function)
  3255. {
  3256.   int speccount = specpdl_depth ();
  3257.   Lisp_Object tem;
  3258.  
  3259.   tem = XSYMBOL (function)->function;
  3260.   if (NILP (tem) || EQ (tem, Qunbound))
  3261.     return (Qnil);
  3262.  
  3263.   specbind (Qinhibit_quit, Qt);
  3264.   /* gc_currently_forbidden = 1; Currently no reason to do this; */
  3265.  
  3266.   /* Qerror not Qt, so you can get a backtrace */
  3267.   tem = condition_case_1 (Qerror,
  3268.                           catch_them_squirmers_call0,
  3269.               function,
  3270.                           caught_a_squirmer,
  3271.               warning_string ? make_opaque_ptr (warning_string) :
  3272.               Qnil);
  3273.  
  3274.   /* gc_currently_forbidden = 0; */
  3275.   return unbind_to (speccount, tem);
  3276. }
  3277.  
  3278. static Lisp_Object
  3279. catch_them_squirmers_call1 (Lisp_Object cons)
  3280. {
  3281.   /* This function can GC */
  3282.   return call1 (XCAR (cons), XCDR (cons));
  3283. }
  3284.  
  3285. Lisp_Object
  3286. call1_trapping_errors (char *warning_string, Lisp_Object function,
  3287.                Lisp_Object object)
  3288. {
  3289.   int speccount = specpdl_depth ();
  3290.   Lisp_Object tem;
  3291.   Lisp_Object cons;
  3292.  
  3293.   tem = XSYMBOL (function)->function;
  3294.   if (NILP (tem) || EQ (tem, Qunbound))
  3295.     return (Qnil);
  3296.  
  3297.   specbind (Qinhibit_quit, Qt);
  3298.   /* gc_currently_forbidden = 1; Currently no reason to do this; */
  3299.  
  3300.   cons = Fcons (function, object);
  3301.   /* Qerror not Qt, so you can get a backtrace */
  3302.   tem = condition_case_1 (Qerror,
  3303.                           catch_them_squirmers_call1,
  3304.               cons,
  3305.                           caught_a_squirmer,
  3306.               warning_string ? make_opaque_ptr (warning_string) :
  3307.               Qnil);
  3308.  
  3309.   /* gc_currently_forbidden = 0; */
  3310.   return unbind_to (speccount, tem);
  3311. }
  3312.  
  3313.  
  3314. /**********************************************************************/
  3315. /*                     The special binding stack                      */
  3316. /**********************************************************************/
  3317.  
  3318. static void
  3319. grow_specpdl (void)
  3320. {
  3321.   if (specpdl_size >= max_specpdl_size)
  3322.     {
  3323.       if (max_specpdl_size < 400)
  3324.     max_specpdl_size = 400;
  3325.       if (specpdl_size >= max_specpdl_size)
  3326.     {
  3327.       if (!NILP (Vdebug_on_error))
  3328.         /* Leave room for some specpdl in the debugger.  */
  3329.         max_specpdl_size = specpdl_size + 100;
  3330.       continuable_error
  3331.         ("Variable binding depth exceeds max-specpdl-size");
  3332.     }
  3333.     }
  3334.   specpdl_size *= 2;
  3335.   if (specpdl_size > max_specpdl_size)
  3336.     specpdl_size = max_specpdl_size;
  3337.   specpdl = ((struct specbinding *)
  3338.          xrealloc (specpdl, specpdl_size * sizeof (struct specbinding)));
  3339.   specpdl_ptr = specpdl + specpdl_depth_counter;
  3340. }
  3341.  
  3342.  
  3343. /* Handle unbinding buffer-local variables */
  3344. static Lisp_Object
  3345. specbind_unwind_local (Lisp_Object ovalue)
  3346. {
  3347.   Lisp_Object current = Fcurrent_buffer ();
  3348.   Lisp_Object symbol = specpdl_ptr->symbol;
  3349.   struct Lisp_Cons *victim = XCONS (ovalue);
  3350.   Lisp_Object buf = get_buffer (victim->car, 0);
  3351.   ovalue = victim->cdr;
  3352.  
  3353.   free_cons (victim);
  3354.  
  3355.   if (NILP (buf))
  3356.     {
  3357.       /* Deleted buffer -- do nothing */
  3358.     }
  3359.   else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0)
  3360.     {
  3361.       /* Was buffer-local when binding was made, now no longer is.
  3362.        *  (kill-local-variable can do this.)
  3363.        * Do nothing in this case.
  3364.        */
  3365.     }
  3366.   else if (EQ (buf, current))
  3367.     Fset (symbol, ovalue);
  3368.   else
  3369.   {
  3370.     /* Urk! Somebody switched buffers */
  3371.     struct gcpro gcpro1;
  3372.     GCPRO1 (current);
  3373.     Fset_buffer (buf);
  3374.     Fset (symbol, ovalue);
  3375.     Fset_buffer (current);
  3376.     UNGCPRO;
  3377.   }
  3378.   return (symbol);
  3379. }
  3380.  
  3381. static Lisp_Object
  3382. specbind_unwind_wasnt_local (Lisp_Object buffer)
  3383. {
  3384.   Lisp_Object current = Fcurrent_buffer ();
  3385.   Lisp_Object symbol = specpdl_ptr->symbol;
  3386.  
  3387.   buffer = get_buffer (buffer, 0);
  3388.   if (NILP (buffer))
  3389.     {
  3390.       /* Deleted buffer -- do nothing */
  3391.     }
  3392.   else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0)
  3393.     {
  3394.       /* Was buffer-local when binding was made, now no longer is.
  3395.        *  (kill-local-variable can do this.)
  3396.        * Do nothing in this case.
  3397.        */
  3398.     }
  3399.   else if (EQ (buffer, current))
  3400.     Fkill_local_variable (symbol);
  3401.   else
  3402.     {
  3403.       /* Urk! Somebody switched buffers */
  3404.       struct gcpro gcpro1;
  3405.       GCPRO1 (current);
  3406.       Fset_buffer (buffer);
  3407.       Fkill_local_variable (symbol);
  3408.       Fset_buffer (current);
  3409.       UNGCPRO;
  3410.     }
  3411.   return (symbol);
  3412. }
  3413.  
  3414.  
  3415. /* Don't want to include buffer.h just for this */
  3416. extern struct buffer *current_buffer;
  3417.  
  3418. void
  3419. specbind (Lisp_Object symbol, Lisp_Object value)
  3420. {
  3421.   int buffer_local;
  3422.  
  3423.   CHECK_SYMBOL (symbol, 0);
  3424.  
  3425.   if (specpdl_depth_counter >= specpdl_size)
  3426.     grow_specpdl ();
  3427.  
  3428.   buffer_local = symbol_value_buffer_local_info (symbol, current_buffer);
  3429.   if (buffer_local == 0)
  3430.     {
  3431.       specpdl_ptr->old_value = find_symbol_value (symbol);
  3432.       specpdl_ptr->func = 0;      /* Handled specially by unbind_to */
  3433.     }
  3434.   else if (buffer_local > 0)
  3435.     {
  3436.       /* Already buffer-local */
  3437.       specpdl_ptr->old_value = Fcons (Fcurrent_buffer (),
  3438.                       find_symbol_value (symbol));
  3439.       specpdl_ptr->func = specbind_unwind_local;
  3440.     }
  3441.   else
  3442.     {
  3443.       /* About to become buffer-local */
  3444.       specpdl_ptr->old_value = Fcurrent_buffer ();
  3445.       specpdl_ptr->func = specbind_unwind_wasnt_local;
  3446.     }
  3447.   
  3448.   specpdl_ptr->symbol = symbol;
  3449.   specpdl_ptr++;
  3450.   specpdl_depth_counter++;
  3451.  
  3452.   Fset (symbol, value);
  3453. }
  3454.  
  3455. void
  3456. record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg),
  3457.                        Lisp_Object arg)
  3458. {
  3459.   if (specpdl_depth_counter >= specpdl_size)
  3460.     grow_specpdl ();
  3461.   specpdl_ptr->func = function;
  3462.   specpdl_ptr->symbol = Qnil;
  3463.   specpdl_ptr->old_value = arg;
  3464.   specpdl_ptr++;
  3465.   specpdl_depth_counter++;
  3466. }
  3467.  
  3468. extern int check_sigio (void);
  3469.  
  3470. Lisp_Object
  3471. unbind_to (int count, Lisp_Object value)
  3472. {
  3473.   int quitf;
  3474.   struct gcpro gcpro1;
  3475.  
  3476.   GCPRO1 (value);
  3477.  
  3478.   check_quit (); /* make Vquit_flag accurate */
  3479.   quitf = !NILP (Vquit_flag);
  3480.   Vquit_flag = Qnil;
  3481.  
  3482.   while (specpdl_depth_counter != count)
  3483.     {
  3484.       Lisp_Object ovalue;
  3485.       --specpdl_ptr;
  3486.       --specpdl_depth_counter;
  3487.  
  3488.       ovalue = specpdl_ptr->old_value;
  3489.       if (specpdl_ptr->func != 0)
  3490.         /* An unwind-protect */
  3491.     (*specpdl_ptr->func) (ovalue);
  3492.       else
  3493.         Fset (specpdl_ptr->symbol, ovalue);
  3494.  
  3495. #ifndef EXCEEDINGLY_QUESTIONABLE_CODE
  3496.       /* There should never be anything here for us to remove.
  3497.      If so, it indicates a logic error in Emacs.  Catches
  3498.      should get removed when a throw or signal occurs, or
  3499.      when a catch or condition-case exits normally.  But
  3500.      it's too dangerous to just remove this code. --ben */
  3501.  
  3502.       /* If we're unwound past the pdlcount of a catch frame,
  3503.          that catch can't possibly still be valid. */
  3504.       while (catchlist && catchlist->pdlcount > specpdl_depth_counter)
  3505.         {
  3506.           catchlist = catchlist->next;
  3507.           /* Don't mess with gcprolist, backtrace_list here */
  3508.         }
  3509. #endif
  3510.     }
  3511.   if (quitf)
  3512.     Vquit_flag = Qt;
  3513.  
  3514.   UNGCPRO;
  3515.  
  3516.   return (value);
  3517. }
  3518.  
  3519.  
  3520. int
  3521. specpdl_depth (void)
  3522. {
  3523.   return (specpdl_depth_counter);
  3524. }
  3525.  
  3526.  
  3527. /* Get the value of symbol's global binding, even if that binding is
  3528.    not now dynamically visible.  May return Qunbound or magic values. */
  3529.  
  3530. Lisp_Object
  3531. top_level_value (symbol)
  3532.      Lisp_Object symbol;
  3533. {
  3534.   REGISTER struct specbinding *ptr = specpdl;
  3535.   CHECK_SYMBOL (symbol, 0);
  3536.   for (; ptr != specpdl_ptr; ptr++)
  3537.     {
  3538.       if (EQ (ptr->symbol, symbol))
  3539.     return ptr->old_value;
  3540.     }
  3541.   return XSYMBOL (symbol)->value;
  3542. }
  3543.  
  3544. #if 0
  3545.  
  3546. Lisp_Object
  3547. top_level_set (symbol, newval)
  3548.      Lisp_Object symbol, newval;
  3549. {
  3550.   REGISTER struct specbinding *ptr = specpdl;
  3551.  
  3552.   CHECK_SYMBOL (symbol, 0);
  3553.   for (; ptr != specpdl_ptr; ptr++)
  3554.     {
  3555.       if (EQ (ptr->symbol, symbol))
  3556.     {
  3557.       ptr->old_value = newval;
  3558.       return newval;
  3559.     }
  3560.     }
  3561.   return Fset (symbol, newval);
  3562. }  
  3563.  
  3564. #endif /* 0 */
  3565.  
  3566.  
  3567. /**********************************************************************/
  3568. /*                            Backtraces                              */
  3569. /**********************************************************************/
  3570.  
  3571. DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
  3572.   "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
  3573. The debugger is entered when that frame exits, if the flag is non-nil.")
  3574.   (level, flag)
  3575.      Lisp_Object level, flag;
  3576. {
  3577.   REGISTER struct backtrace *backlist = backtrace_list;
  3578.   REGISTER int i;
  3579.  
  3580.   CHECK_INT (level, 0);
  3581.  
  3582.   for (i = 0; backlist && i < XINT (level); i++)
  3583.     {
  3584.       backlist = backlist->next;
  3585.     }
  3586.  
  3587.   if (backlist)
  3588.     backlist->debug_on_exit = !NILP (flag);
  3589.  
  3590.   return flag;
  3591. }
  3592.  
  3593. static void
  3594. backtrace_specials (int speccount, int speclimit, Lisp_Object stream)
  3595. {
  3596.   int printing_bindings = 0;
  3597.  
  3598.   for (; speccount > speclimit; speccount--)
  3599.     {
  3600.       if (specpdl[speccount - 1].func == 0
  3601.           || specpdl[speccount - 1].func == specbind_unwind_local
  3602.           || specpdl[speccount - 1].func == specbind_unwind_wasnt_local)
  3603.     {
  3604.       write_c_string (((!printing_bindings) ? "  # bind (" : " "),
  3605.               stream);
  3606.       Fprin1 (specpdl[speccount - 1].symbol, stream);
  3607.       printing_bindings = 1;
  3608.     }
  3609.       else
  3610.     {
  3611.       if (printing_bindings) write_c_string (")\n", stream);
  3612.       write_c_string ("  # (unwind-protect ...)\n", stream);
  3613.       printing_bindings = 0;
  3614.     }
  3615.     }
  3616.   if (printing_bindings) write_c_string (")\n", stream);
  3617. }
  3618.  
  3619. DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 2, "",
  3620.   "Print a trace of Lisp function calls currently active.\n\
  3621. Option arg STREAM specifies the output stream to send the backtrace to,\n\
  3622. and defaults to the value of `standard-output'.  Optional second arg\n\
  3623. DETAILED means show places where currently active variable bindings,\n\
  3624. catches, condition-cases, and unwind-protects were made as well as\n\
  3625. function calls. ")
  3626.   (stream, detailed)
  3627.   Lisp_Object stream, detailed;
  3628. {
  3629.   struct backtrace *backlist = backtrace_list;
  3630.   struct catchtag *catches = catchlist;
  3631.   int speccount = specpdl_depth_counter;
  3632.  
  3633.   int old_nl = print_escape_newlines;
  3634.   int old_pr = print_readably;
  3635.   Lisp_Object old_level = Vprint_level;
  3636.   Lisp_Object oiq = Vinhibit_quit;
  3637.   struct gcpro gcpro1, gcpro2;
  3638.  
  3639.   /* We can't allow quits in here because that could cause the values
  3640.      of print_readably and print_escape_newlines to get screwed up.
  3641.      Normally we would use a record_unwind_protect but that would
  3642.      screw up the functioning of this function. */
  3643.   Vinhibit_quit = Qt;
  3644.  
  3645.   entering_debugger = 0;
  3646.  
  3647.   Vprint_level = make_number (3);
  3648.   print_readably = 0;
  3649.   print_escape_newlines = 1;
  3650.  
  3651.   GCPRO2 (stream, old_level);
  3652.  
  3653.   if (NILP (stream))
  3654.     stream = Vstandard_output;
  3655.   if (!noninteractive && (NILP (stream) || EQ (stream, Qt)))
  3656.     stream = Fselected_frame (Qnil);
  3657.  
  3658.   for (;;)
  3659.     {
  3660.       if (!NILP (detailed) && catches && catches->backlist == backlist)
  3661.     {
  3662.           int catchpdl = catches->pdlcount;
  3663.           if (specpdl[catchpdl].func == condition_case_unwind
  3664.               && speccount > catchpdl)
  3665.             /* This is a condition-case catchpoint */
  3666.             catchpdl = catchpdl + 1;
  3667.              
  3668.           backtrace_specials (speccount, catchpdl, stream);
  3669.  
  3670.           speccount = catches->pdlcount;
  3671.           if (catchpdl == speccount)
  3672.         {
  3673.           write_c_string ("  # (catch ", stream);
  3674.           Fprin1 (catches->tag, stream);
  3675.           write_c_string (" ...)\n", stream);
  3676.         }
  3677.           else
  3678.             {
  3679.               write_c_string ("  # (condition-case ... . ", stream);
  3680.               Fprin1 (Fcdr (Fcar (catches->tag)), stream);
  3681.               write_c_string (")\n", stream);
  3682.             }
  3683.           catches = catches->next;
  3684.     }
  3685.       else if (!backlist)
  3686.     break;
  3687.       else
  3688.     {
  3689.       if (!NILP (detailed) && backlist->pdlcount < speccount)
  3690.         {
  3691.           backtrace_specials (speccount, backlist->pdlcount, stream);
  3692.           speccount = backlist->pdlcount;
  3693.         }
  3694.       write_c_string (((backlist->debug_on_exit) ? "* " : "  "),
  3695.               stream);
  3696.       if (backlist->nargs == UNEVALLED)
  3697.         {
  3698.           Fprin1 (Fcons (*backlist->function, *backlist->args), stream);
  3699.         }
  3700.       else
  3701.         {
  3702.           Lisp_Object tem = *backlist->function;
  3703.           Fprin1 (tem, stream); /* This can QUIT */
  3704.           write_c_string ("(", stream);
  3705.           if (backlist->nargs == MANY)
  3706.         {
  3707.           int i;
  3708.           Lisp_Object tail = Qnil;
  3709.           struct gcpro gcpro1;
  3710.  
  3711.           GCPRO1 (tail);
  3712.           for (tail = *backlist->args, i = 0;
  3713.                !NILP (tail);
  3714.                tail = Fcdr (tail), i++)
  3715.             {
  3716.               if (i != 0) write_c_string (" ", stream);
  3717.               Fprin1 (Fcar (tail), stream);
  3718.             }
  3719.           UNGCPRO;
  3720.         }
  3721.           else
  3722.         {
  3723.           int i;
  3724.           for (i = 0; i < backlist->nargs; i++)
  3725.             {
  3726.               if (i != 0) write_c_string (" ", stream);
  3727.               Fprin1 (backlist->args[i], stream);
  3728.             }
  3729.         }
  3730.         }
  3731.       write_c_string (")\n", stream);
  3732.       backlist = backlist->next;
  3733.     }
  3734.     }
  3735.   Vprint_level = old_level;
  3736.   print_readably = old_pr;
  3737.   print_escape_newlines = old_nl;
  3738.   UNGCPRO;
  3739.   Vinhibit_quit = oiq;
  3740.   return Qnil;
  3741. }
  3742.  
  3743.  
  3744. DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
  3745.   "Return the function and arguments N frames up from current execution point.\n\
  3746. If that frame has not evaluated the arguments yet (or is a special form),\n\
  3747. the value is (nil FUNCTION ARG-FORMS...).\n\
  3748. If that frame has evaluated its arguments and called its function already,\n\
  3749. the value is (t FUNCTION ARG-VALUES...).\n\
  3750. A &rest arg is represented as the tail of the list ARG-VALUES.\n\
  3751. FUNCTION is whatever was supplied as car of evaluated list,\n\
  3752. or a lambda expression for macro calls.\n\
  3753. If N is more than the number of frames, the value is nil.")
  3754.   (nframes)
  3755.      Lisp_Object nframes;
  3756. {
  3757.   REGISTER struct backtrace *backlist = backtrace_list;
  3758.   REGISTER int i;
  3759.   Lisp_Object tem;
  3760.  
  3761.   CHECK_NATNUM (nframes, 0);
  3762.  
  3763.   /* Find the frame requested.  */
  3764.   for (i = XINT (nframes); backlist && (i-- > 0);)
  3765.     backlist = backlist->next;
  3766.  
  3767.   if (!backlist)
  3768.     return Qnil;
  3769.   if (backlist->nargs == UNEVALLED)
  3770.     return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
  3771.   else
  3772.     {
  3773.       if (backlist->nargs == MANY)
  3774.     tem = *backlist->args;
  3775.       else
  3776.     tem = Flist (backlist->nargs, backlist->args);
  3777.  
  3778.       return Fcons (Qt, Fcons (*backlist->function, tem));
  3779.     }
  3780. }
  3781.  
  3782.  
  3783. /**********************************************************************/
  3784. /*                            Warnings                                */
  3785. /**********************************************************************/
  3786.  
  3787. void
  3788. warn_when_safe_lispstr (Lisp_Object class, Lisp_Object level,
  3789.             Lisp_Object str)
  3790. {
  3791.   str = list1 (list3 (class, level, str));
  3792.   if (NILP (Vpending_warnings))
  3793.     Vpending_warnings = Vpending_warnings_tail = str;
  3794.   else
  3795.     {
  3796.       Fsetcdr (Vpending_warnings_tail, str);
  3797.       Vpending_warnings_tail = str;
  3798.     }
  3799. }
  3800.  
  3801. /* #### This should probably accept Lisp objects; but then we have
  3802.    to make sure that Feval() isn't called, since it might not be safe. */
  3803.  
  3804. void
  3805. warn_when_safe (Lisp_Object class, Lisp_Object level, CONST char *fmt, ...)
  3806. {
  3807.   Lisp_Object obj;
  3808.   va_list args;
  3809.  
  3810.   va_start (args, fmt);
  3811.   obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt),
  3812.                 Qnil, -1, args);
  3813.   va_end (args);
  3814.  
  3815.   warn_when_safe_lispstr (class, level, obj);
  3816. }
  3817.  
  3818.  
  3819.  
  3820.  
  3821. /**********************************************************************/
  3822. /*                          Initialization                            */
  3823. /**********************************************************************/
  3824.  
  3825. void
  3826. syms_of_eval (void)
  3827. {
  3828.   defsymbol (&Qinhibit_quit, "inhibit-quit");
  3829.   defsymbol (&Qautoload, "autoload");
  3830.   defsymbol (&Qdebug_on_error, "debug-on-error");
  3831.   defsymbol (&Qstack_trace_on_error, "stack-trace-on-error");
  3832.   defsymbol (&Qdebug_on_signal, "debug-on-signal");
  3833.   defsymbol (&Qstack_trace_on_signal, "stack-trace-on-signal");
  3834.   defsymbol (&Qdebugger, "debugger");
  3835.   defsymbol (&Qmacro, "macro");
  3836.   defsymbol (&Qand_rest, "&rest");
  3837.   defsymbol (&Qand_optional, "&optional");
  3838.   /* Note that the process code also uses Qexit */
  3839.   defsymbol (&Qexit, "exit");
  3840.   defsymbol (&Qsetq, "setq");
  3841. #ifndef standalone
  3842.   defsymbol (&Qinteractive, "interactive");
  3843.   defsymbol (&Qcommandp, "commandp");
  3844.   defsymbol (&Qdefun, "defun");
  3845.   defsymbol (&Qeval, "eval");
  3846.   defsymbol (&Qprogn, "progn");
  3847.   defsymbol (&Qvalues, "values");
  3848. #endif
  3849.   defsymbol (&Qdisplay_warning, "display-warning");
  3850.  
  3851.   defsubr (&Sor);
  3852.   defsubr (&Sand);
  3853.   defsubr (&Sif);
  3854.   defsubr (&Scond);
  3855.   defsubr (&Sprogn);
  3856.   defsubr (&Sprog1);
  3857.   defsubr (&Sprog2);
  3858.   defsubr (&Ssetq);
  3859.   defsubr (&Squote);
  3860.   defsubr (&Sfunction);
  3861.   defsubr (&Sdefun);
  3862.   defsubr (&Sdefmacro);
  3863.   defsubr (&Sdefvar);
  3864.   defsubr (&Sdefconst);
  3865.   defsubr (&Suser_variable_p);
  3866.   defsubr (&Slet);
  3867.   defsubr (&SletX);
  3868.   defsubr (&Swhile);
  3869.   defsubr (&Smacroexpand);
  3870.   defsubr (&Scatch);
  3871.   defsubr (&Sthrow);
  3872.   defsubr (&Sunwind_protect);
  3873.   defsubr (&Scondition_case);
  3874.   defsubr (&Scall_with_condition_handler);
  3875.   defsubr (&Ssignal);
  3876. #ifndef standalone
  3877.   defsubr (&Sinteractive_p);
  3878.   defsubr (&Scommandp);
  3879.   defsubr (&Scommand_execute);
  3880. #endif
  3881.   defsubr (&Sautoload);
  3882.   defsubr (&Seval);
  3883.   defsubr (&Sapply);
  3884.   defsubr (&Sfuncall);
  3885.   defsubr (&Sbacktrace_debug);
  3886.   defsubr (&Sbacktrace);
  3887.   defsubr (&Sbacktrace_frame);
  3888. }
  3889.  
  3890. void
  3891. reinit_eval (void)
  3892. {
  3893.   specpdl_ptr = specpdl;
  3894.   specpdl_depth_counter = 0;
  3895.   catchlist = 0;
  3896.   Vcondition_handlers = Qnil;
  3897.   backtrace_list = 0;
  3898.   Vquit_flag = Qnil;
  3899.   debug_on_next_call = 0;
  3900.   lisp_eval_depth = 0;
  3901.   entering_debugger = 0;
  3902. }
  3903.  
  3904. void
  3905. vars_of_eval (void)
  3906. {
  3907.   DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
  3908.     "Limit on number of Lisp variable bindings & unwind-protects before error.");
  3909.  
  3910.   DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
  3911.     "Limit on depth in `eval', `apply' and `funcall' before error.\n\
  3912. This limit is to catch infinite recursions for you before they cause\n\
  3913. actual stack overflow in C, which would be fatal for Emacs.\n\
  3914. You can safely make it considerably larger than its default value,\n\
  3915. if that proves inconveniently small.");
  3916.  
  3917.   DEFVAR_LISP ("quit-flag", &Vquit_flag,
  3918.     "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
  3919. Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
  3920.   Vquit_flag = Qnil;
  3921.  
  3922.   DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
  3923.     "Non-nil inhibits C-g quitting from happening immediately.\n\
  3924. Note that `quit-flag' will still be set by typing C-g,\n\
  3925. so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
  3926. To prevent this happening, set `quit-flag' to nil\n\
  3927. before making `inhibit-quit' nil.  The value of `inhibit-quit' is\n\
  3928. ignored if a critical quit is requested by typing control-shift-G in\n\
  3929. an X frame.");
  3930.   Vinhibit_quit = Qnil;
  3931.  
  3932.   DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
  3933.     "*Non-nil means automatically display a backtrace buffer\n\
  3934. after any error that is not handled by a `condition-case'.\n\
  3935. If the value is a list, an error only means to display a backtrace\n\
  3936. if one of its condition symbols appears in the list.\n\
  3937. See also variable `stack-trace-on-signal'.");
  3938.   Vstack_trace_on_error = Qnil;
  3939.  
  3940.   DEFVAR_LISP ("stack-trace-on-signal", &Vstack_trace_on_signal,
  3941.     "*Non-nil means automatically display a backtrace buffer\n\
  3942. after any error that is signalled, whether or not it is handled by\n\
  3943. a `condition-case'.\n\
  3944. If the value is a list, an error only means to display a backtrace\n\
  3945. if one of its condition symbols appears in the list.\n\
  3946. See also variable `stack-trace-on-error'.");
  3947.   Vstack_trace_on_signal = Qnil;
  3948.  
  3949.   DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
  3950.     "*Non-nil means enter debugger if an unhandled error is signalled.\n\
  3951. The debugger will not be entered if the error is handled by\n\
  3952. a `condition-case'.\n\
  3953. If the value is a list, an error only means to enter the debugger\n\
  3954. if one of its condition symbols appears in the list.\n\
  3955. See also variables `debug-on-quit' and `debug-on-signal'.");
  3956.   Vdebug_on_error = Qnil;
  3957.  
  3958.   DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
  3959.     "*Non-nil means enter debugger if an error is signalled.\n\
  3960. The debugger will be entered whether or not the error is handled by\n\
  3961. a `condition-case'.\n\
  3962. If the value is a list, an error only means to enter the debugger\n\
  3963. if one of its condition symbols appears in the list.\n\
  3964. See also variable `debug-on-quit'.");
  3965.   Vdebug_on_signal = Qnil;
  3966.  
  3967.   DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
  3968.     "*Non-nil means enter debugger if quit is signalled (C-G, for example).\n\
  3969. Does not apply if quit is handled by a `condition-case'.  Entering the\n\
  3970. debugger can also be achieved at any time (for X11 devices) by typing\n\
  3971. control-shift-G to signal a critical quit.");
  3972.   debug_on_quit = 0;
  3973.  
  3974.   DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
  3975.     "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
  3976.  
  3977.   DEFVAR_LISP ("debugger", &Vdebugger,
  3978.     "Function to call to invoke debugger.\n\
  3979. If due to frame exit, args are `exit' and the value being returned;\n\
  3980.  this function's value will be returned instead of that.\n\
  3981. If due to error, args are `error' and a list of the args to `signal'.\n\
  3982. If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
  3983. If due to `eval' entry, one arg, t.");
  3984.   Vdebugger = Qnil;
  3985.  
  3986.   DEFVAR_LISP ("run-hooks", &Vrun_hooks,
  3987.     "Set to the function `run-hooks', if that function has been defined.\n\
  3988. Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
  3989.   Vrun_hooks = Qnil;
  3990.  
  3991.   staticpro (&Vpending_warnings);
  3992.   Vpending_warnings = Qnil;
  3993.   Vpending_warnings_tail = Qnil; /* no need to protect this */
  3994.  
  3995.   in_warnings = 0;
  3996.  
  3997.   staticpro (&Vautoload_queue);
  3998.   Vautoload_queue = Qnil;
  3999.  
  4000.   staticpro (&Vcondition_handlers);
  4001.  
  4002.   specpdl_size = 50;
  4003.   specpdl_depth_counter = 0;
  4004.   specpdl = (struct specbinding *)
  4005.     xmalloc (specpdl_size * sizeof (struct specbinding));
  4006.   /* XEmacs change: increase these values. */
  4007.   max_specpdl_size = 1500;
  4008.   max_lisp_eval_depth = 500;
  4009.   throw_level = 0;
  4010.  
  4011.   reinit_eval ();
  4012. }
  4013.  
  4014.  
  4015.